Learning experiment leveraging Terraform to automate the infrastructure deployments & Packer to automate baking both the EC2 AMI image and Docker images. In additon pushing the docker image to ECR (AWS Elastic Container Registry) via Packer post-processor.
*Prerequisites:
- Have Terraform, Packer, & Docker installed on your machine.*
- Have an AWS account.
Directory: ./infrastructure/terraform/shared
cd ./infrastructure/terraform/shared && \
terraform init && \
terraform apply --auto-approve
Create an ./infrastructure/packer/images/variables.auto.pkrvars.hcl file
aws_account_id = "<YOUR AWS ACCOUNT ID>"
ecr_repository = "terraform-packer-docker-project"
region = "us-east-1"
Directory: ./infrastructure/packer/images
packer build -var-file="./variables.auto.pkrvars.hcl" ec2.pkr.hcl
You can alternatively build with regular Dockerfile(s) and script to tag and push to ECR. This is automated with Packer post-processors
Directory: ./infrastructure/packer/images
packer build -var-file="./variables.auto.pkrvars.hcl" docker.pkr.hcl
Since the AWS AMI is built and the Docker image has been pushed to ECR, can deploy EC2 application servers
This terraform configuration is setup for blue/green deployments. To start out only need one of the blue/green servers.
./infrastructure/terraform/application
terraform init && \
terraform apply \
-var "traffic_distribution=blue" \
-var "enable_green_env=false" \
--auto-approve
The blue-green-deployment.sh file is setup to script toggling between blue and green deployments, can reference the traffic_distribution
output value and determine which was previously promoted as production servers.
- Fork the repository
- Create a free Terraform Cloud Account
- Generate a TFE User Token
- Create two CLI powered workspaces for
terraform-shared
&terraform-application
- Update both Cloud Blocks with your own Terraform Cloud Organization name, current placeholder:
<TFC ORGANIZATION HERE>
- Have an AWS Account and AWS credentials handy
- Add the following Github Actions Environment Secrets
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_ACCOUNT_ID
(AWS Account ID)TF_API_TOKEN
(From Terraform Cloud)