Cloudformation conditions yaml

0

I have to create an auto scaling group in two regions and the only difference between the two are the subnets. us-east-1 has 1 subnet whereas us-east-2 has two. How can I use the condition to call the subnet value from region map. Here's my broken code. Any help is appreciated.

Mappings:
  RegionMap:
    us-east-1:
      AMI: "ami-066f487d3b6819b0d"
      Subnet1: "subnet-0e6f12f64042ea5b1"
    us-east-2:
      AMI: "ami-0aef5e0adcbc7cc0f"
      Subnet1: "subnet-0e6f12f64042ea5b1"
      Subnet2: "subnet-0bc661bb8d98f3f03"

Conditions:
  region: !Equals [!Ref us-east-2, Subnet2]

  autoscaling:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      AutoScalingGroupName: asg1
      VPCZoneIdentifier: 
        - !FindInMap [RegionMap, !Ref "AWS::Region", Subnet1]
        - If regions = us-east-2 then !FindInMap [RegionMap, !Ref "AWS::Region", Subnet2] # This is what I need to figure out

I couldn't find any examples of this. Has anyone used a region map and used conditionals with it?

asked a year ago347 views
2 Answers
0

How about this? I have not tested the template.

Mappings:
  RegionMap:
    us-east-1:
      AMI: "ami-066f487d3b6819b0d"
      Subnets: ["subnet-0e6f12f64042ea5b1"]
    us-east-2:
      AMI: "ami-0aef5e0adcbc7cc0f"
      Subnets: ["subnet-0e6f12f64042ea5b1", "subnet-0bc661bb8d98f3f03"]

Resources:
  autoscaling:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      AutoScalingGroupName: asg1
      VPCZoneIdentifier: !FindInMap [RegionMap, !Ref "AWS::Region", Subnets]

I added the following on 2022-12-10.

I verified that the stack creation completes successfully using the following template. I confirmed with us-east-1 and us-east-2. Note: I changed the AMI IDs and Subnet IDs to the values of my AWS account.

AWSTemplateFormatVersion: "2010-09-09"
Mappings:
  RegionMap:
    us-east-1:
      AMI: "ami-0b0dcb5067f052a63"
      Subnets: ["subnet-xxxxx28b"]  # mask
    us-east-2:
      AMI: "ami-0beaa649c482330f7"
      Subnets: ["subnet-xxxxxb61", "subnet-xxxxx9e5"]  # mask

Resources:
  autoscaling:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      LaunchTemplate:
        LaunchTemplateId: !Ref myLaunchTemplate
        Version: !GetAtt myLaunchTemplate.LatestVersionNumber
      MaxSize: "1"
      MinSize: "0"
      DesiredCapacity: "1"
      VPCZoneIdentifier: !FindInMap [RegionMap, !Ref "AWS::Region", Subnets]
  myLaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: launch-template-test
      LaunchTemplateData:
        ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
        InstanceType: t2.micro
profile picture
answered a year ago
  • Thank you for your response. I had initially tried it but ran it a problem while creating it with the following error.

    "Value of property VPCZoneIdentifier must be of type List of String"

  • @learner00 I added the template I verified to my answer. The template worked as expected. If you could share the entire template with us, it might help us figure out the cause of your error.

0

Hello, for error "Value of property VPCZoneIdentifier must be of type List of String", consider removing [ ] enclosing the subnets in the Mappings section. Also, the following template can be used in both us-east-1 and us-east-2 regions that you can consider testing:

Note: Displayed limited resource properties for simplicity. Make sure to add the required properties and replace your subnets IDs and AMI IDs.

Mappings:
  RegionMap:
    us-east-1:
      AMI: ami-xxxx
      Subnets: "subnet-xxxx"
    us-east-2:
      AMI: ami-xxxx
      Subnets: "subnet-xxxx, subnet-xxxxx"
Parameters:
  region:
    Type: String
    AllowedValues:
      - us-east-1
      - us-east-2
Conditions:
  UE1: !Equals 
    - !Ref region
    - us-east-1
Resources:
  autoscaling:
    Type: 'AWS::AutoScaling::AutoScalingGroup'
    Properties:
      AutoScalingGroupName: name-of-asg
      VPCZoneIdentifier:
        - !If 
          - UE1
          - !FindInMap 
            - RegionMap
            - !Ref region
            - Subnets
          - !FindInMap 
            - RegionMap
            - !Ref region
            - Subnets
 #with other required properties in this resource type
AWS
SUPPORT ENGINEER
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions