Rrj17
Rrj17

Reputation: 53

Developing an app to create a stack from a cloudformation template

I am new to AWS and am currently working on simple tasks.

I have created a free tier EC2 instance using a cloudformation template. Now my next task is to write a simple application that uses respective AWS SDK to call CloudFormation API to create a stack from the template.

Here is the cloudformation template:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Ec2 Template",
"Metadata": {
    "Copyright":[
        "Copyright 2017, All rights reserved"
        ],
        "Comments":[
            "Create an EC2"
        ]
},
"Parameters": {
    "KeyName": {
        "Type": "AWS::EC2::KeyPair::KeyName",
        "Description": "Name of an existing EC2 KeyPair to enable access to join ECS instances."
    },
"InstanceTypeParameter":{
    "Type": "String",
    "Default": "t2.micro",
    "AllowedValues": [
    "t2.micro",
    "m1.small",
    "m1.large"
    ],
    "Description": "Enter t2.micro, m1.small, or m1.large. Default is t2.micro."
},
"EcsSecurityGroupLb":{
    "Type": "AWS::EC2::SecurityGroup::Id",
    "Description":"The ECS ELB Security Group."
},
"vpcid":{
    "Type": "AWS::EC2::VPC::Id"
},
"mySubnetIDs": {
    "Description":"Subnet IDs",
    "Type":"AWS::EC2::Subnet::Id"
}
   },
    "Resources":{
        "Ec2Instance":{
            "Type":"AWS::EC2::Instance",
            "Properties":{
                "ImageId": "ami-bf4193c7",
                "KeyName": {
                "Ref": "KeyName"
            },
            "InstanceType":{
                "Ref": "InstanceTypeParameter"
            },

            "NetworkInterfaces":[
              { 
                "AssociatePublicIpAddress":"true",
                "DeviceIndex":"0",
                "SubnetId":{
                    "Ref":"mySubnetIDs"
                },
                "GroupSet":[
                    {
                        "Ref": "EcsSecurityGroupLb"
                    }
                ]
            }
        ],
            "BlockDeviceMappings":[
                {
                    "DeviceName": "/dev/sdc",
                    "VirtualName":"ephemeral0"
                }
            ]
        }
    }
},
"Outputs":{
   "Ec2Instance":{
    "Description": "InstanceId of newly created EC2 instance",
    "Value": {
        "Ref": "Ec2Instance"
        }
    },
    "InstanceIPAddress":{
    "Value":{ "Fn::GetAtt": ["Ec2Instance", "PublicIp"]},
    "Description": "Public IP address of instance"
    }        
  }
}           

I have gone through a lot of documentation but haven't really understood as to how to proceed. I would like to know if there are any good tutorials on this.

Looking for suggestions with respect to the steps as well.

Thanks!

Upvotes: 1

Views: 146

Answers (1)

Himal
Himal

Reputation: 3512

Since you have to (as a task requirement) write the application yourself, you'll need to use one of the AWS SDKs that are available.

The SDK you choose will depend on what programming language you are most comfortable using (or required by your task).

Roughly, your program will need to do the following:

  1. With the AWS SDK, create an AWS session which uses your IAM user's API keys.
  2. Grab the Cloudformation template off of your local system.
  3. With the AWS SDK, invoke Cloudformation to create the resource stack with your template.
  4. (Optionally) Wait until the stack is complete and output a report on the stack status.

Good luck!

Upvotes: 1

Related Questions