Anas Anjaria
Anas Anjaria's blog

Follow

Anas Anjaria's blog

Follow
Tip for Creating AWS Elastic Beanstalk Environment Using Terraform

Photo by Vipul Jha on Unsplash

Tip for Creating AWS Elastic Beanstalk Environment Using Terraform

Anas Anjaria's photo
Anas Anjaria
·Sep 6, 2022·

2 min read

I got an opportunity to create an elastic beanstalk environment using Terraform for our production system. However, most tutorials on the internet only cover creating a simple environment, that is, an environment without a load balancer, autoscaling group, etc.

As I started working on this task, I made a mistake by creating an autoscaling group, load balancer, etc., explicitly using terraform.

If you read the official guideline of AWS, it says

Elastic Beanstalk defines a large number of configuration options that you can use to configure your environment’s behavior and the resources that it contains.

This means that we only need to set the correct configuration options and AWS manages everything for us.

// code copied from https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elastic_beanstalk_environment#example-with-options

resource "aws_elastic_beanstalk_environment" "tfenvtest" {  
  ...  

  // all you need is to define require settings here  
  setting {  
    namespace = "aws:ec2:vpc"  
    name      = "VPCId"  
    value     = "vpc-xxxxxxxx"  
  }  

  setting {  
    namespace = "aws:ec2:vpc"  
    name      = "Subnets"  
    value     = "subnet-xxxxxxxx"  
  }  
}

You can find available options for all environments here.

Tip— Reverse engineering

The traditional approach

write a code first to create an environment and set the correct setting by reading the official documentation. It could be time-consuming.

Another approach is reverse engineering .

Create an environment using AWS console and generate a code from that environment using terraform import command.

terraform import aws_elastic_beanstalk_environment.prodenv <your-env-id>

Please note that the import command will produce a lot of code, so you may need to adjust or remove unwanted code. In my opinion, it is still faster.

I found the following resources very helpful for understanding terraform import command.

Tip — Naming conventions for your resources

As we are creating resources via terraform, it will be a good idea to follow some naming conventions for our resources.

This article recommends cloud naming conventions as follows.

[prefix]-[project]-[env]-[resource]-[location]-[description]-[suffix]
Cloud naming convention description
Snapshot by the author — Source: stepan.wtf/cloud-naming-convention
[prefix] Organization name for e.g ggl for Google  
[project] Project name for e.g webmonitor  
[env] p for production, s for staging, t for testing, d for development  
[resource] As I have only used AWS resource, so it would be rds, ec2, iam etc  
[location] Location of the resource for e.g euwe1c for eu-west-1c  
[description] Additional description for e.g primary, accesscontrol etc  
[suffix] - for instance 1xa3

So it would be

- ggl-webmonitor-t-rds-euwe1c-primary-1xa3  
- ggl-webmonitor-d-sg-euwe1c-accesscontrol-1ya4

Thanks for reading.


Originally posted on medium

 
Share this