AWS Cloudformation 将条件函数解释为资源 属性
AWS Cloudformation interprets conditionnal function as a resource property
我在使用 cloudformation 模板时遇到了一个奇怪的行为。这是我的模板,我在其中创建了一个存储桶并希望根据条件进行通知配置:
AWSTemplateFormatVersion: '2010-09-09'
Description: "Setup Artifacts Bucket"
Parameters:
BucketName:
Description: Name of the pipeline setup arctifact bucket
Type: String
Default: "s3-pipeline-setup"
NotificationCondition:
Description: Conditionally add Notification configuration to the artifact bucket
Type: String
Default: false
Conditions:
AddNotificationConfiguration: !Equals [ !Ref NotificationCondition, true ]
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
Fn::If:
- AddNotificationConfiguration
-
NotificationConfiguration:
LambdaConfigurations:
-
Function: "arn:aws:lambda:eu-west-1:341292222222227:function:lambda-ops-trigger-pipeline-setup"
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: prefix
Value: "appstackcodes/"
-
Name: suffix
Value: "txt"
- !Ref AWS::NoValue
当我尝试部署时失败并出现此错误:
00:28:10
UTC+0200 CREATE_FAILED AWS::S3::Bucket ArtifactBucket Encountered
unsupported property Fn::If
我不太明白这件事..有人可以试试让我知道那里的错误吗?
谢谢
不幸的是,您无法在 cloudformation 中执行您想要的操作。
Fn::If
基本上可以用作三元表达式。例如
key: Fn::If: [condition_name, value_if_true, value_if_false]
它不能像在编程语言中那样用作逻辑流。有很多方法可以解决。您实际上似乎已经发现了 AWS::NoValue
,所以只需将 NotificationConfiguration
赋值移动到 if
.
之外即可
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
NotificationConfiguration:
Fn::If:
- AddNotificationConfiguration
- LambdaConfigurations:
-
Function: "arn:aws:lambda:eu-west-1:341294322147:function:lambda-itops-trigger-pipeline-setup"
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: prefix
Value: "appstackcodes/"
-
Name: suffix
Value: "txt"
- !Ref AWS::NoValue
实际上,您总是在为 NotificationConfiguration
分配一些内容,但有时这很神奇 AWS::NoValue
。这在大多数情况下都有效,尽管有时这还不够,需要更多的创造力!
我在使用 cloudformation 模板时遇到了一个奇怪的行为。这是我的模板,我在其中创建了一个存储桶并希望根据条件进行通知配置:
AWSTemplateFormatVersion: '2010-09-09'
Description: "Setup Artifacts Bucket"
Parameters:
BucketName:
Description: Name of the pipeline setup arctifact bucket
Type: String
Default: "s3-pipeline-setup"
NotificationCondition:
Description: Conditionally add Notification configuration to the artifact bucket
Type: String
Default: false
Conditions:
AddNotificationConfiguration: !Equals [ !Ref NotificationCondition, true ]
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
Fn::If:
- AddNotificationConfiguration
-
NotificationConfiguration:
LambdaConfigurations:
-
Function: "arn:aws:lambda:eu-west-1:341292222222227:function:lambda-ops-trigger-pipeline-setup"
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: prefix
Value: "appstackcodes/"
-
Name: suffix
Value: "txt"
- !Ref AWS::NoValue
当我尝试部署时失败并出现此错误:
00:28:10 UTC+0200 CREATE_FAILED AWS::S3::Bucket ArtifactBucket Encountered unsupported property Fn::If
我不太明白这件事..有人可以试试让我知道那里的错误吗?
谢谢
不幸的是,您无法在 cloudformation 中执行您想要的操作。
Fn::If
基本上可以用作三元表达式。例如
key: Fn::If: [condition_name, value_if_true, value_if_false]
它不能像在编程语言中那样用作逻辑流。有很多方法可以解决。您实际上似乎已经发现了 AWS::NoValue
,所以只需将 NotificationConfiguration
赋值移动到 if
.
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
NotificationConfiguration:
Fn::If:
- AddNotificationConfiguration
- LambdaConfigurations:
-
Function: "arn:aws:lambda:eu-west-1:341294322147:function:lambda-itops-trigger-pipeline-setup"
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: prefix
Value: "appstackcodes/"
-
Name: suffix
Value: "txt"
- !Ref AWS::NoValue
实际上,您总是在为 NotificationConfiguration
分配一些内容,但有时这很神奇 AWS::NoValue
。这在大多数情况下都有效,尽管有时这还不够,需要更多的创造力!