Hello Readers! In this blog we will see how we can deploy ElasticBeanstalk using terraform. In my previous blog we have seen how to deploy applications in Elastic Beanstalk. There we have seen how AWS takes care of all underlying resources so you don’t have to worry about anything. You can purely concentrate on writing your application code, package it and upload it. And EBS takes care of its infrastructure and resources.
Prerequisites:
- You must have terraform installed in your system. If you haven’t then prefer this blog for the installation of terraform: https://learn.hashicorp.com/tutorials/terraform/install-cli
Check the terraform version:
It’s successfully install in your system. It’s time to build Terraform configuration files for AWS Elastic beanstalk that we will use to launch AWS Elastic beanstalk on the AWS Cloud.
So, let’s get started!
Step1: Make a directory and move to this directory. Create a file name main.tf and paste the entire content in that file. For deploying ElasticBeanstalk we need to create an ElasticBeanstalk application and environment. So, In this file we are creating an application and environment.
# Create elastic beanstalk application
resource "aws_elastic_beanstalk_application" "elasticapp" {
name = var.elasticapp
}
# Create elastic beanstalk Environment
resource "aws_elastic_beanstalk_environment" "beanstalkappenv" {
name = var.beanstalkappenv
application = aws_elastic_beanstalk_application.elasticapp.name
solution_stack_name = var.solution_stack_name
tier = var.tier
setting {
namespace = "aws:ec2:vpc"
name = "VPCId"
value = var.vpc_id
}
setting {
namespace = "aws:autoscaling:launchconfiguration"
name = "IamInstanceProfile"
value = "aws-elasticbeanstalk-ec2-role"
}
setting {
namespace = "aws:ec2:vpc"
name = "AssociatePublicIpAddress"
value = "True"
}
setting {
namespace = "aws:ec2:vpc"
name = "Subnets"
value = join(",", var.public_subnets)
}
setting {
namespace = "aws:elasticbeanstalk:environment:process:default"
name = "MatcherHTTPCode"
value = "200"
}
setting {
namespace = "aws:elasticbeanstalk:environment"
name = "LoadBalancerType"
value = "application"
}
setting {
namespace = "aws:autoscaling:launchconfiguration"
name = "InstanceType"
value = "t2.medium"
}
setting {
namespace = "aws:ec2:vpc"
name = "ELBScheme"
value = "internet facing"
}
setting {
namespace = "aws:autoscaling:asg"
name = "MinSize"
value = 1
}
setting {
namespace = "aws:autoscaling:asg"
name = "MaxSize"
value = 2
}
setting {
namespace = "aws:elasticbeanstalk:healthreporting:system"
name = "SystemType"
value = "enhanced"
}
}
Step2: After that we will create a vars.tf file in the same directory. As the name suggests this file will contain all the variables that we will refer to in the main.tf file. Paste the following content in your file.
variable "elasticapp" {
default = "myapplication"
}
variable "beanstalkappenv" {
default = "myenvironment"
}
variable "solution_stack_name" {
default = "64bit Amazon Linux 2018.03 v2.10.13 running Python 3.6"
}
variable "tier" {
default = "WebServer"
}
variable "vpc_id" {
default = "vpc-0dd76a41523810a28"
}
variable "public_subnets" {
default = ["subnet-091f546a60cdf204f", "subnet-0af22fcd735aea012"]
}
In this file I have mentioned application name, environment name, solution stack name, tier, vpc_id and public subnets. You can change these fields as per your need. If you don’t know what to give the solution stack name then you can choose this by listing it. The command for listing solution stack name is:
$ aws elasticbeanstalk list-available-solution-stacks
Step3: Now, we will make a file named provider.tf. As its name suggests in this file we will mention where to create the resources. The provider.tf file will authenticate and allow Terraform to connect to the AWS cloud.
provider "aws" {
region = "ap-south-1"
}
Step4: All the configurations are done now. It’s time to use the terraform command to deploy the ElasticBeanstalk. So, the first command we will use is:
$ terraform init
This will initialize the terraform. Terraform was initialized successfully.
Step5: Next we will run the following command which tells the terraform what are the resources that need to be provision.
$ terraform plan
Step6: Finally we will run the command that will deploy our ElasticBeanstalk.
$ terraform apply
This will take some time, let’s wait.
Yes, it’s successfully done now. Now, let’s check the ElasticBeanstalk in our AWS console.
It’s my ElasticBeanstalk environment.
This is my application:
When you open that url in your environment you will find your deployed application.
Conclusion
Thank you for sticking to the end. In this blog we have seen how we can deploy ElasticBeanstalk using terraform. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.
HAPPY LEARNING!