Create an AWS Beanstalk Environment using Terraform

A code example

Despina Papatheodorou
3 min readJul 21, 2022

Intro

terraform
/ˈtɛrəfɔːm/
verb:
terraform something to make a planet more like Earth, so that people can live on it

Oops not this kind of terraform..

If you are a DevOps Engineer or you have widen responsibilities inside your team, probably you have needed to create a server on cloud , or a pipeline or whatever..
And if you use AWS cloud, wow so many configurations and screens etc. mess.

Here comes the Terraform, an infrastructure as code tool that let you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.

In other words, Terraform is a DevOps tool that you can use to design, create, destroy infrastructure.
Example given you can create applications, environments, networks, servers, pipelines etc.

Terraform provides support for manage resources on Amazon Web Services (AWS), Azure, Google Cloud Platform (GCP), Kubernetes etc.

For more info you can check the official terraform website.

Few months ago, in my team we faced the problem to automate our deployment process and we needed more environments to test the new deliverables with QA team and partners. The environments should be identical and have same build and deployment flow.
So we decided to use Terraform to create new applications, environments and code pipelines on AWS cloud.

In this post, I will step on the first part of the solution we followed and I will describe the procedure how to build an AWS Beanstalk environment.

Prerequisite:

  • Before we start Download Terraform
  • Assume also that you already have already an AWS account.
    So you already have an:

access_key
secret_key
region

We need these credentials in order to login on AWS cloud.

Starting:

Let’s see what resources we need to create :
1. An aws elastic beanstalk application
2. An aws elastic beanstalk environment

May you want to associate an aws key pair .
A key pair (public key, private key) is used to control login access to EC2 instances. Anyone with the private key, you can SSH into the EC2 machine .

Check here how can create one

We declare all resources inside the resources.tf file and all the variables inside the var.tf file.

So on resources.tf we want to create the elastic beanstalk application and the elastic beanstalk environment that will run a specific application version. Remember more than one environments can belong to one application.

# Create elastic beanstalk application resource “aws_elastic_beanstalk_application” “beanstalk-app” { 
name = var.beanstalk_app_name
}
# Create elastic beanstalk Environment resource “aws_elastic_beanstalk_environment” “beanstalk-app-env” {
name = var.beanstalk_env_name
application = var.beanstalk_app_name
solution_stack_name = var.solution_stack_name
<... add environment properties... >
}

As you see , we define
the name of the environment,
the application name,
the solution_stack_name which specifies the platform will be used , in my example “64bit Amazon Linux 2018.03 v3.4.19 running Tomcat 8.5 Java 8” (check the supproted EB platforms here)
and of course all the environment properties, as is the aws credentials, the database configuration (here a postgres configuration) etc

The environment properties (in Environment Configuration) can be declared “static”

setting {
namespace = “aws:elasticbeanstalk:application:environment”
name = “AWS_ACCESS_KEY_ID”
value = var.aws_access_key
}

or “dynamic”

dynamic “setting” {
for_each = var.settings
content {
namespace = setting.value[“namespace”]
name = setting.value[“name”]
value = setting.value[“value”]
}
}

Go to action

Now we have created the resources.tf and var.tf file, we will execute the terraform commands to create the infrastructure.

On terminal, locate to folder that you have created these files.

First of all we need to initialise Terraform

terraform init

Now, terraform should creates an execution plan, detects the changes needed which and lets you preview them . The below command will show the resources that will be created, versioned, or scaled down

terraform plan

If everything looks fine to you, time to apply the changes. Terraform executes the actions scripted in the resources.tf file and creates the infrastructure.

terraform apply

Now assume that you want to destroy all remote objects managed by the configuration file (resources.tf ), execute

terraform destroy

You may want to destroy only some parts of infrastructure, e.g. to destroy the beanstalk environment or the the beanstalk application

terraform destroy -target=aws_elastic_beanstalk_environment.beanstalk-app-envterraform destroy -target=aws_elastic_beanstalk_application.beanstalk-app

A more complete guide of how you can destroy resources in respect of terraform state you can check how-to-destroy-terraform-resources.

You can check the example source code on GitHub .

Happy “colonize” AWS :))

--

--