AWS cloudformation intro

Pre-requisites

  • Install AWS CLI.
  • Configure it.
    1
    aws configure
  • Check it worked.
    1
    aws s3 ls

First contact with templates

  • Minimum example correction.
    • Initial script.
      1
      2
      3
      4
      5
      6
      7
      aws cloudformation create-stack \
      --template-body file:\\lamp-as.json \
      --stack-name lamp \
      --parameters \
      ParameterKey=KeyName,ParameterValue=mykey
      # won't work, needs more network elements information
      # such as VPCs, subNets info and DB access
    • Retrieve the lacking information from the CLI.
      1
      2
      aws ec2 describe-vpcs
      aws ec2 describe-subnets
    • Complete template.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      aws cloudformation create-stack \
      --template-body file:\\lamp-as.json \
      --stack-name lamp-as \
      --parameters \
      ParameterKey=KeyName,
      ParameterValue=mykey \
      ParameterKey=VpcId, \
      ParameterValue=vpc-1ffbc964 \
      ParameterKey=Subnets, \
      ParameterValue=\'subnet-0e170b31,subnet-52d6117c\' \
      ParameterKey=DBUser, \
      ParameterValue=myadmin \
      ParameterKey=DBPassword, \
      ParameterValue=mypass23
    • Once it is launched, verify.
      • Cloudformation is running.
        1
        aws cloudformation describe-stacks
      • EC2 instances are running.
        1
        2
        3
        4
        aws ec2 describe-instances \
        --filters Name=instance-state-name,Values=running \
        --query 'Reservations[*].Instances[*]\
        .{Instance:InstanceId,PublicIPAddress:PublicIpAddress}'

Get help for filling up more AWS fields

  • Retrieve information about stacks.
    1
    aws cloudformation describe-stacks
  • Define the EC2 instance type on template.
    • You may swap that default value for any of the other AllowedValues, or override it from the CLI.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      "Parameters" : {
      "KeyName": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
      },

      "InstanceType" : {
      "Description" : "WebServer EC2 instance type",
      "Type" : "String",
      "Default" : "t2.small",
    • Check Mappings section,to review available hardware architectures and AMI identifiers for each region.
      • Optional section, you can insert your own non-standard values organized into key\value pairs.
        1
        2
        3
        4
        5
        "Mappings" : {
        "AWSInstanceType2Arch" : {
        "t1.micro" : { "Arch" : "HVM64" },
        "t2.nano" : { "Arch" : "HVM64" },
        "t2.micro" : { "Arch" : "HVM64" },
      • The Resources section in this case defines your instance environment (public IP address is associated with the new Elastic IP address that will be allocated).
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        "Resources" : {
        # ...
        "InstanceSecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
        "GroupDescription" : "Enable SSH access via port 22",
        "SecurityGroupIngress" : [ {
        "IpProtocol" : "tcp",
        "FromPort" : "22",
        "ToPort" : "22",
        "CidrIp" : { "Ref" : "SSHLocation"}
        } ]
        }
        }
        },

Get help with external stacks

  • Quick Starts - pre-built infrastructure stacks that are provided here to help you create more complex cloud deployments by third-party companies to simplify the process of building their infrastructure in within the AWS platform.