Cloudformation Template

0

So I am making cloud formation template main.yaml file and one other yaml file from which parameters can be taken so that I can reuse it by just making changes in the value of another yaml file where only values are other but facing issue. Please guide accordingly. Parameters: InstanceType: Description: EC2 instance type Type: String Default: t2.micro AllowedValues: [t2.micro, t2.small, t2.medium, m4.large] SecurityGroupId: Description: Security group ID for the EC2 instance Type: AWS::EC2::SecurityGroup::Id VpcId: Description: VPC ID Type: AWS::EC2::VPC::Id KeyName: Description: Name of the key pair to use for SSH access Type: AWS::EC2::KeyPair::KeyName SubnetId: Description: Subnet ID for the EC2 instance Type: AWS::EC2::Subnet::Id

Resources: EC2Instance: Type: AWS::EC2::Instance Properties: InstanceType: !Ref InstanceType SecurityGroupIds: [!Ref SecurityGroupId] KeyName: !Ref KeyName SubnetId: !Ref SubnetId ImageId: ami-0f8ca728008ff5af4 UserData: !Base64 Fn::Sub: | #!/bin/bash sudo apt-get update sudo apt install apache2 -y sudo systemctl start apache2 sudo systemctl enable apache2 Like we ave variables in terraform in params.yaml How do I format and define these values or take user input InstanceType: t2.micro KeyName: devops SecurityGroupIds: sg-02464c840862fddaf SubnetId: subnet-0b2bbe1a860c1ec8f KeyName: devops VpcId: vpc-01491099ac5c6857a Facing issue with formatting and defining paramsyaml file.Please guide.

  • Please copy code blocks instead of pictures and also please add the issue you have faced

  • Please guide. how to do it in cloudformation like variables.tf we do in terraform

user01
asked a year ago369 views
1 Answer
0

Hi There

First, you need to store the parameters in a json file (you cant use YAML for the parameters file). Example parameters.json file:

[
   {
        "ParameterKey": "InstanceType",
        "ParameterValue": "t2.micro"
    },
    {
        "ParameterKey": "SecurityGroupId",
        "ParameterValue": "sg-xxxxxxxx"
    }
]

Then, you deploy your CloudFormation stack using the --parameter-overrides option.

aws cloudformation deploy --template-file /path_to_template/template.yml --stack-name my-new-stack --parameter-overrides file://path_to_parameters/parameters.json

Reference: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/deploy/index.html

profile pictureAWS
EXPERT
Matt-B
answered a year ago
profile pictureAWS
EXPERT
reviewed a year ago
  • Thank you. What about Userdata can it be also passed instead of template.yaml from parameters.json? If yes, How? please guide with full
    This is my template.yaml AWSTemplateFormatVersion: "2010-09-09" Parameters: InstanceType: Type: String Default: t2.micro Description: EC2 instance type AMI: Type: AWS::EC2::Image::Id Default: ami-0f8ca728008ff5af4 Description: ID of the Amazon Machine Image (AMI) to use for the instance KeyName: Type: AWS::EC2::KeyPair::KeyName Default: my-key-pair Description: Name of an existing EC2 KeyPair to enable SSH access to the instance SecurityGroupId: Description: Security group ID for the EC2 instance Type: AWS::EC2::SecurityGroup::Id VpcId: Description: VPC ID Type: AWS::EC2::VPC::Id SubnetId: Description: Subnet ID for the EC2 instance Type: AWS::EC2::Subnet::Id RootVolumeSize: Type: Number Default: 0 Description: Size of the root volume in GB DataVolumeSize: Type: Number Default: 0 Description: Size of the data volume in GB Resources: EC2Instance: Type: AWS::EC2::Instance Properties: InstanceType: !Ref InstanceType ImageId: !Ref AMI KeyName: !Ref KeyName SecurityGroupIds: - !Ref SecurityGroupId SubnetId: !Ref SubnetId UserData: !Base64 Fn::Sub: | #!/bin/bash sudo apt-get update sudo apt install apache2 -y sudo systemctl start apache2

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