DEV Community

Cover image for Deploy Elastic Beanstalk Application with Terraform
Emre Oztoprak for AWS Community Builders

Posted on

Deploy Elastic Beanstalk Application with Terraform

AWS Elastic Beanstalk is a managed service for deploying and scaling web applications and services. It supports languages such as Java, .NET, PHP, Node.js, Python, Ruby, Go, and also Docker. You can deploy these applications on Apache, Nginx, Passenger, and IIS.

But Elastic Beanstalk has a lot of configurations. With Terraform we can automate this process. Later we can use the same Terraform script just changing variables.

With this Terraform configuration, we will create PHP 8.0 Elastic Beanstalk WebServer Environment on Amazon Linux 2 and Nginx. Also Application Load Balancer and SSL certificate for our domain. We will validate this certificate, create A record for our domain and it will be pointed to Elastic Beanstalk Environment. In the end, it will create Cloudwatch alarms for Elastic Beanstalk Environment Health, LoadBalancer 5xx requests, and network out the traffic of our instances.

Image description

Structure of our Terraform Configurations

Requirements

Create EC2 Key Pair 
Creating key pair in the EC2 console and using the name of that key pair in Terraform is a secure way than passing the public key pair file in Terraform.

SNS Topic
When Cloudwatch alarms are triggered you will get notifications via SNS. You can add subscriptions like E-Mail, SMS, or HTTP-HTTPS endpoint like Opsgenie. You can use the same SNS Topic later in different alarms.

Route53 Public Hosted Zone
SSL Certification and DNS Records for your domain

Deployment

Clone the repository.

git@github.com:emreoztoprak/terraform-aws-elasticbeanstalk.git
Enter fullscreen mode Exit fullscreen mode

Change the variables in the terraform.tfvars file

I set up one subnet for EC2 Instance and two subnets for ALB. You can change this any number you want.

Image description

When everything is ready you can deploy with these 4 commands.

terraform init
terraform validate
terraform plan
terraform apply

Enter fullscreen mode Exit fullscreen mode

Finally :)
Image description

Github repo

I hope it was useful. Thank you for reading this article.

Latest comments (0)