A working demo application created using Packer, Ansible, Inspec and Terraform, deployed to AWS.
The purpose of this demo app is to show a working example of these tools working together.
The end result is a simple Hello World script running on an EC2 instance on AWS.
-
Packer is used to create an Amazon Machine Image (AMI). An AMI is like a prepared EC2 instance that has not been started up yet.
-
Ansible is used within Packer to install some neccessary services while Packer is building the image.
-
Inspec is used within Packer also, to perform some verification steps to make sure Packer and Ansible have created the Image as expected.
-
Terraform is used to create the minimum AWS infrastructure we need. It will use the Image created by Packer and create a small running EC2 instance within a new VPC.
- You will need an AWS account and your AWS account ID
- Packer installed locally
- Terraform installed locally
- Inspec installed locally
Use the following steps to create a new user in your AWS account and give it permission to create EC2 instances and Route53 zones. This will be used by Packer and Terraform to create an AMI and an EC2 instance.
aws iam create-user --user-name example
aws iam create-access-key --user-name example
- ( Make sure to save the AccessKeyId and SecretAccessKey from the output)
aws iam attach-user-policy --policy-arn arn:aws:iam::aws:policy/AmazonEC2FullAccess --user-name example
aws iam attach-user-policy --policy-arn arn:aws:iam::aws:policy/AmazonRoute53FullAccess --user-name example
- Create a file at ~/.aws/credentials with the following content:
[example]
aws_access_key_id = Your_AccessKeyId
aws_secret_access_key = Your_SecretAccessKey
- Copy your public key so Terraform can use it to create a .pem file which you can use to SSH in to the EC2 instance if needed:
cat ~/.ssh/id_rsa.pub > ../terraform/files/id_rsa.pub
- Clone this repo
- Add your AWS Account ID to terraform/terraform.tfvars
- Validate Packer using :
packer validate -var-file=packer/variables.json packer/server.json
- Build the AMI with Packer using :
packer build -var-file=packer/variables.json packer/server.json
- Deploy the image with Terraform using:
cd /terraform
terraform init
terraform apply
terraform destroy
aws iam delete-user --user-name example
First, get the AMI ID value using:
aws ec2 describe-images --filters "Name=tag:Name,Values=example.com" --profile=example --region=eu-west-1 --query 'Images[*].{ID:ImageId}'
aws ec2 deregister-image --image-id ami-<value>