简体   繁体   English

如何在 CloudFormation 模板中将多个 URL 传递给 AllowOrigins

[英]How to Pass Multiple URLs to AllowOrigins in CloudFormation Template

I am using a CloudFormation template in YML format.我正在使用 YML 格式的 CloudFormation 模板。

Depending on the environment, I need to be able to use different URLs for the Allowed Origins attribute of my CorsConfiguration.根据环境,我需要能够为我的 CorsConfiguration 的 Allowed Origins 属性使用不同的 URL。 Ideally, I would like to use a Parameter defined like this:理想情况下,我想使用这样定义的参数:

  AllowedOrigins:
    Description: Allowed Origins
    Type: String
    AllowedPattern: '.+'

I have tried to pass in a delimited string (ie "http://localhost:4200,http://localhost:4201"), and split the values like this:我试图传入一个分隔字符串(即“http://localhost:4200,http://localhost:4201”),并像这样拆分值:

  OnboardingHttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      CorsConfiguration:
        AllowOrigins: !Split [ ",", !Ref AllowedOrigins ]

The response in CloudFormation is: CloudFormation 中的响应是:

Warnings found during import: CORS Scheme is malformed, ignoring.导入时发现警告:CORS Scheme is malformed, ignoring. (Service: AmazonApiGatewayV2; Status Code: 400; Error Code: BadRequestException; Request ID: 21072c02-70c3-473d-9629-784005226bd4; Proxy: null) (Service: null; Status Code: 404; Error Code: BadRequestException; Request ID: null; Proxy: null) (服务:AmazonApiGatewayV2;状态代码:400;错误代码:BadRequestException;请求 ID:21072c02-70c3-473d-9629-784005226bd4;代理:null)(服务:null;状态代码:404;错误代码:BadRequestException;请求 ID: null;代理:空)

This is the answer I got from AWS Support:这是我从 AWS Support 得到的答案:

The Split function is for splitting strings into a list, but it is not for referencing an attribute. Split function 用于将字符串拆分为列表,但不用于引用属性。 It is designed to be used with the Select function or other functions.它旨在与 Select function 或其他功能一起使用。 So it is not a stand-alone function to be used for referencing.所以不是单机function来做参考。 For this, you can use the CommaDelimitedList parameter type.为此,您可以使用 CommaDelimitedList 参数类型。 You can use the CommaDelimitedList parameter type to specify multiple string values in a single parameter.您可以使用 CommaDelimitedList 参数类型在单个参数中指定多个字符串值。 Once you pass the CommaDelimitedList parameter value, you can reference it later on your template.传递 CommaDelimitedList 参数值后,您可以稍后在模板中引用它。 Here is a CloudFormation template that works:这是一个有效的 CloudFormation 模板:

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Parameters:
 AllowedOriginsURLs:
   Type: CommaDelimitedList
   Default: 'https://example.com,https://example2.com'
   Description: Please enter your URLs
Resources:
 HttpApi:
   Type: 'AWS::Serverless::HttpApi'
   Properties:
     StageName: my-stage-name
     Tags:
       Tag: MyTag
     StageVariables:
       StageVar: Value
     CorsConfiguration:
       AllowOrigins: !Ref AllowedOriginsURLs
       AllowHeaders: [ x-apigateway-header ]
       AllowMethods: [ GET ]
       MaxAge: 600
       AllowCredentials: true

The AllowedOriginsURLs parameter is of type CommaDelimitedList, with the default being 'http://localhost:4200,http://localhost:4201'. AllowedOriginsURLs 参数的类型为 CommaDelimitedList,默认值为“http://localhost:4200,http://localhost:4201”。 You can change this parameter on startup, then you can reference AllowedOriginsURLs on AllowOrigins.您可以在启动时更改此参数,然后您可以在 AllowOrigins 上引用 AllowedOriginsURLs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一个 CloudFormation 模板中声明多个相似的资源组? - How to declare multiple and similar groups of resources in the same CloudFormation template? 用于创建多个 SQS 的 Cloudformation 模板 - Cloudformation template to create multiple SQS 如何在 AWS CloudFormation 模板中使用参考号 function? - How is the !Ref function used in an AWS CloudFormation template? 如何将“手动创建”的资源添加到 cloudformation 模板 - how to add "manually created" resources to the cloudformation template 从 CloudFormation 模板中的 DropDownList 选择多个值 - Selecting multiple values from DropDownList in CloudFormation Template 用于多个参数文件和单个模板的 CloudFormation - CloudFormation for multiple parameter files and a single template 如何将 CloudFormation 输出传递到 CodeBuild 阶段? - How To Pass CloudFormation Outputs To A CodeBuild Stage? 如何在 AWS CloudFormation 模板中运行 bash 脚本 - How to run a bash script in a AWS CloudFormation template 如何识别定义了 IAM 角色的 CloudFormation 模板? - How to identify CloudFormation template that defined IAM role? 如何在 CfnInclude 期间将(可选)参数直接从 CloudFormation 模板传递到 CDK 资源而不对参数列表进行硬编码? - How can I pass (optional) parameters directly from CloudFormation template to CDK resource during CfnInclude without hardcoding the parameter list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM