Skip to content

Infrastructure provisioning of AWS EC2 instances using Terraform, Cloudformation, AWS Console, aws cli

Notifications You must be signed in to change notification settings

colinbut/microservice-aws-ec2-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Microservice AWS EC2 Setup

This project provides the steps/methods required for setup an EC2 instance for running the microservice as defined in the microservice-aws-demo project.

The microservice can be a containerized or a non-containerized microservice. See microservice-java & microservice-nodejs projects for more details on this.

There are many ways to setup EC2 instances in AWS:

  1. via AWS mamagement console creating from scratch
  2. using Infrastructure as Code tool (Terraform/Cloudformation)
  3. AWS CLI
  4. utilizing a pre-build custom made AMI for any of above methods

Assumptions

  1. Assumes a AWS account created and AWS credentials configured (access_keys & secret_keys etc)

  2. This project will create EC2 instances in the default VPC.

  3. To put in a custom non-default VPC require to create the custom non-default VPC. See my Terraform project - aws-public-private-vpc for more reference on to create this.

Option 1 - AWS Management Console

Follow the official AWW guide from AWS documentation - https://docs.aws.amazon.com/efs/latest/ug/gs-step-one-create-ec2-resources.html

Option 2 - Terraform/Cloudformation

It is good to manage infrastructure changes and define them in 'code'. This option details how to do this via modern infrastructure as code tools such as Terraform and AWS Cloudformation

Terraform

Create S3 bucket to store terraform state. Need to ensure bucket is unique and also ideally within same region you want to provision your infrastructure resources onto.

aws s3 mb s3://[name of bucket] --region [region]

Initialize Terraform Backend

terraform init

Validating the terraform configuration files syntax is correct:

terraform validate

When make changes it is always a good idea to run terraform plan to check the changes are what you intend to make... even though terraform apply does prompt you confirm (if run without the --auto-approve option)

terraform plan

To apply your infrastructure changes (creation/modifications)

terraform apply

Cloudformation

Go to the Cloudformation directory and execute following command from the AWS CLI:

aws cloudformation create-stack --stack-name microservice-ec2-instance --template-body file://ec2_instances.yml --parameters ParameterKey=KeyName,ParameterValue=MyLondonKP

the --stack-name you can provide anything.

Option 3 - AWS CLI

Can also create the required EC2 instances via the AWS CLI

There's a pre-made create_ec2_instances script (available in both Bash & Python) that wraps around the AWS CLI command call. To use:

create_ec2_instances.sh

or using Python:

python create_ec2_instances.py

The scripts basically just does the following aws cli command underneath the hood:

aws ec2 run-instances --image-id [ami-id] --count 1 --instance-type t2.micro --key-name [KeyPair Name] --user-data [provisioning script]

Option 4 - from pre-build custom made AMI

See the microservice-ami project for more details on the corresponding AMIs for the required development platform.

Possible to uses Packer to build the AMI.

To build & deploy to your AWS account:

e.g.

packer build [microservice-*.json]

The AMI can now be referenced in any of the above 3 methods of creating the EC2 instance.