Skip to main content

CloudFormation

sample template of CloudFormation in yaml format

$ cat mystack.yml
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  FirstVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      Tags:
      - Key: Name
        Value: myfirststack

validate template file

$ aws cloudformation validate-template --template-body file://mystack.yml

create stack

$ aws cloudformation create-stack --stack-name mystack --template-body file://mystack.yml

confrim information of created stack

$ aws cloudformation list-stacks
$ aws cloudformation describe-stacks --stack-name mystack
$ aws cloudformation describe-stack-events --stack-name mystack --query 'StackEvents[?ResourceType==`AWS::CloudFormation::Stack`]'

$ aws cloudformation describe-stack-resources --stack-name mystack
$ aws cloudformation list-stack-resources --stack-name mystack

$ aws cloudformation get-template --stack-name mystack
$ aws cloudformation get-template --stack-name mystack | jq -r '.TemplateBody'
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  FirstVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      Tags:
      - Key: Name
        Value: first-VPC

update template file and validate it

$ diff -u mystack.yml{,.bak}
--- mystack.yml 2019-09-16 11:23:04.368417527 +0900
+++ mystack.yml.bak     2019-09-16 10:51:35.053928361 +0900
@@ -1,12 +1,9 @@
 AWSTemplateFormatVersion: '2010-09-09'
-Parameters:
-  CidrBlock:
-    Type: String
 Resources:
   FirstVPC:
     Type: AWS::EC2::VPC
     Properties:
-      CidrBlock: !Ref CidrBlock
+      CidrBlock: 10.0.0.0/16
       Tags:
       - Key: Name
-        Value: mystack-VPC
+        Value: first-VPC

$ aws cloudformation validate-template --template-body file://mystack.yml

update the stack

$ aws cloudformation update-stack --stack-name mystack --template-body file://mystack.yml --parameters ParameterKey=CidrBlock,ParameterValue=10.0.0.0/17

confirm

delete the stack

$ aws cloudformation delete-stack --stack-name mystack

create stack with ansible

$ cat mystack.yml 
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  FirstVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.10.0.0/16
      Tags:
      - Key: Name
        Value: mystack

$ cat playbook.yml 
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  tasks:
    - name: create a cloudformation stack
      cloudformation:
        stack_name: "mystack"
        state: "present"
        region: "ap-northeast-1"
        disable_rollback: true
        template: "mystack.yml"

$ ansible-playbook --syntax-check playbook.yml 
$ ansible-playbook --list-tasks playbook.yml 
$ ansible-playbook --check playbook.yml
$ ansible-playbook playbook.yml

update stack with ansible

$ cat mystack.yml
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  CidrBlock:
    Type: String
Resources:
  FirstVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref CidrBlock
      Tags:
      - Key: Name
        Value: mystack

$ cat playbook.yml 
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  tasks:
    - name: create a cloudformation stack
      cloudformation:
        stack_name: "mystack"
        state: "present"
        region: "ap-northeast-1"
        disable_rollback: true
        template: "mystack.yml"
        template_parameters:
          CidrBlock: "10.20.0.0/16"

$ ansible-playbook playbook.yml

variable from command line

$ cat mystack.yml
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  CidrBlock:
    Type: String
Resources:
  FirstVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref CidrBlock
      Tags:
      - Key: Name
        Value: mystack

$ cat playbook.yml
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  tasks:
    - name: create a cloudformation stack
      cloudformation:
        stack_name: "mystack"
        state: "present"
        region: "ap-northeast-1"
        disable_rollback: true
        template: "mystack.yml"
        template_parameters:
          CidrBlock: "{{ CidrBlock }}"

$ ansible-playbook -e "CidrBlock=10.30.0.0/16" playbook.yml

delete stack with ansible

$ cat playbook.yml
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  tasks:
    - name: delete a cloudformation stack
      cloudformation:
        stack_name: "mystack"
        state: "absent"
        region: "ap-northeast-1"

$ ansible-playbook playbook.yml