[英]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.