- Newest
- Most votes
- Most comments
The right way to do that is to have VPCId being an SSM parameter : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ssm-parameter.html
You will use !GetAtt Value to obtain its value: see final section of the above
Then you can drive its value with your !If statement to VpcA or VpcB by using the !If construct that you already have.
I am using this myself quite often
You may even go on a simpler path if you need VPCIOd only once. You can insert the condition directly at the place were you need id.
See https://blog.shikisoft.com/aws-cloudformation-no-value-pseudo-parameter/
SampleInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
SubnetId: !Ref WebServerSubnet
ImageId: !Ref ImageId
SecurityGroupIds:
- !If [ EnvironmentIsProduction, !Ref SecurityGroup, !Ref 'AWS::NoValue' ]
Unfortunately decided against using the SSM Parameters route. However I have found you can get the effect I was looking for without.
So to Address that point I have added all the parameters bunch that I needed in Mappings. Those mappings can be addressed afterwards within the same cloudformation template
ExecutionRoleArn:
!If [BranchNameEqualsTest,
!FindInMap [BranchParameters, test, 'ClusterTaskRole'],
!FindInMap [BranchParameters, dev, 'ClusterTaskRole']]
You may be able to use the PyPlate example Macro - see https://github.com/awslabs/aws-cloudformation-templates/blob/master/aws/services/CloudFormation/MacrosExamples/PyPlate/README.md.
I haven't in any way tested this code, but once you make the PyPlate macro available, you could use something roughly like:
Transform: [PyPlate]
Parameters:
VpcA:
Type: String
Default: "vpc-redacteda"
VpcB:
Type: String
Default: "vpc-redactedb"
VPCid:
Type: String
Description: VPC Used for the cluster position
Default: |
#!PyPlate
isTest = ... (Get this from "params" dict or a condition in the "template" dict - not clear to me where you're getting this from)
output = params[VPCid]
if not output.startswith('vpc-'):
output = params[VpcA] if isTest else params[VpcB]
Relevant content
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 6 months ago
Happy that you found the right solution for your use case. Thanks for having accepted my answer