Giter VIP home page Giter VIP logo

cloudformation-demo's Introduction

Cloudformation Demo

1. Creating a basic template


Let's create a single EC2 instance! To help us, available resources are documented and we can look up properties and values etc. See documentation at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html.

Take a look at file 1-basic-ec2.yml

You can spin this up from the command line (on Linux) with the following commands:

$ aws cloudformation create-stack --stack-name "Demo-Stack-Steve" --template-body file://1-basic-ec2.yaml
$ aws cloudformation list-stacks --max-items 5

Problems:

  • Hard coded type (t2.micro), may want to change later
  • Whole bunch of stuff we didn't specify
    • Tags
    • Security Group
    • VPC/Subnet

Let's fix these in our next template...

2. Updating the template and stack


Take a look at file 2-updated-ec2.yml

In this new template we pull out the Instance Type into a parameter to let the user decide at deploy-time. We've also added Tags to both the stack and our EC2 Resource.

Note that our EC2 instance will be terminated and recreated when we change the type, but will remain and simply be updated if we just change the name.

$ aws cloudformation update-stack --stack-name "Demo-Stack-Steve" --template-body file://2-updated-ec2.yml --parameters ParameterKey=InstanceTypeParameter,ParameterValue=t2.medium --tags Key=Owner,Value=StevenChallis

Problems

  • Our subnet is hard coded now, how can I deploy this in multiple accounts?
  • Hard coded AMI won't work in another region

3. Specifying mappings


Take a look at file 3-mappings-ec2.yml

We've now pulled out subnet and AMI into a mapping so it can change based on the region. We'll also turn on detailed monitoring only if we're deploying to Production, basic if not.

$ aws cloudformation update-stack --stack-name "Demo-Stack-Steve" --template-body file://3-mappings-ec2.yml --parameters ParameterKey=InstanceTypeParameter,ParameterValue=t2.medium,ParameterKey=Environment,ParameterValue=Prod --tags Key=Owner,Value=StevenChallis

Problems: Our solution isn't Highly Available. We likely want multiple instances of our application spread across Availability Zones. We might even just want this in Production but not in Development.

4. Autoscaling EC2 instances


Let's replace our single instance with an Autoscaling Group of instances whose size depends on the environment and customer demand. Notice how most of the instance configuration is tranferred as-is to the Launch Config.

Take a look at file 4-autoscaling-ec2.yml

$ aws cloudformation update-stack --stack-name "Demo-Stack-Steve" --template-body file://4-autoscaling-ec2.yml --parameters ParameterKey=InstanceTypeParameter,ParameterValue=t2.medium,ParameterKey=Environment,ParameterValue=Prod --tags Key=Owner,Value=StevenChallis

5. Delete the stack


One advantage of a stack is that it makes it easy to remove a collection of resources when we are done. Let's see this in action by deleting our stack... we know we can easily recreate it!

$ aws cloudformation delete-stack --stack-name "Demo-Stack"

6. Now, let's construct one your workloads


It might be helpful to draw out the components to help determine what we will need in our template.

You can build upon these templates and add features such as:

  • A Load Balancer
  • An Output specifying the URL of the load balancer
  • A userdata script to bootstrap the EC2 instance

Further Learning

See more examples of templates for common scenarios at https://aws.amazon.com/quickstart/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.