Giter Club home page Giter Club logo

terraform-elastic-beanstalk-nodejs's Introduction

Terraform AWS Elastic Beanstalk NodeJS

Terraform script to setup AWS Elastic Beanstalk with a load-balanced NodeJS app

What this script does

  • Create an Elastic Beanstalk Application and environment.
  • Setup the EB environment with NodeJS, an Elastic Loadbalancer and forward port from HTTP / HTTPS to the specified instance port.
  • (Optionnal) Create a Route53 Alias to link your domain to the EB domain name
  • (Optionnal) Create a Cloudfront distribution on top of your Elastic Beanstalk environment

Usage

Create a main.tf file with the following configuration:

Create an EB environment

##################################################
## Your variables
##################################################
variable "aws_region" {
  type    = "string"
  default = "eu-west-1"
}
variable "service_name" {
  type    = "string"
  default = "nodejs-app-test"
}
variable "service_description" {
  type    = "string"
  default = "My awesome nodeJs App"
}
##################################################
## AWS config
##################################################
provider "aws" {
  region = "${var.aws_region}"
}

##################################################
## Elastic Beanstalk config
##################################################
resource "aws_elastic_beanstalk_application" "eb_app" {
  name        = "${var.service_name}"
  description = "${var.service_description}"
}

module "eb_env" {
  source = "github.com/BasileTrujillo/terraform-elastic-beanstalk-nodejs//eb-env"
  aws_region = "${var.aws_region}"

  # Application settings
  service_name = "nodejs-app-test"
  service_description = "My awesome nodeJs App"
  env = "dev"

  # Instance settings
  instance_type = "t2.micro"
  min_instance = "2"
  max_instance = "4"

  # ELB
  enable_https = "false" # If set to true, you will need to add an ssl_certificate_id (see L70 in app/variables.tf)

  # Security
  vpc_id = "vpc-xxxxxxx"
  vpc_subnets = "subnet-xxxxxxx"
  elb_subnets = "subnet-xxxxxxx"
  security_groups = "sg-xxxxxxx"
}

Link your domain using Route53

Add to the previous script the following lines:

##################################################
## Route53 config
##################################################
module "app_dns" {
  source      = "github.com/BasileTrujillo/terraform-elastic-beanstalk-nodejs//r53-alias"
  aws_region  = "${var.aws_region}"

  domain      = "example.io"
  domain_name = "my-app.example.io"
  eb_cname    = "${module.app.eb_cname}"
}

Add an Elastic File System to your EB

  • First, create the EFS before the Elastic Beanstalk Environment:
##################################################
## AWS Elastic File System
##################################################
resource "aws_efs_file_system" "file_storage" {
  creation_token = "${var.service_name}-${var.env}"

  tags {
    Name = "${var.service_name}-${var.env}"
  }
}

resource "aws_efs_mount_target" "file_storage_target" {
  file_system_id  = "${aws_efs_file_system.file_storage.id}"
  subnet_id       = "${var.vpc_subnets}"
  security_groups = ["${var.security_groups}"]
}
  • Then set efs_id & efs_mount_directory to the Elastic Beanstalk Environment module:
##################################################
## Elastic Beanstalk config
##################################################
module "eb_env" {
   source       = "github.com/BasileTrujillo/terraform-elastic-beanstalk-nodejs//eb-env"
    aws_region  = "${var.aws_region}"
  
    # Application settings
    service_name          = "nodejs-app-test"
    service_description   = "My awesome nodeJs App"
    env                   = "dev"
  
    # Instance settings
    instance_type = "t2.micro"
    min_instance  = "2"
    max_instance  = "4"
  
    # ELB
    enable_https  = "false" # If set to true, you will need to add an ssl_certificate_id (see L70 in app/variables.tf)
  
    # Security
    vpc_id            = "vpc-xxxxxxx"
    vpc_subnets       = "subnet-xxxxxxx"
    elb_subnets       = "subnet-xxxxxxx"
    security_groups   = "sg-xxxxxxx"
  
    # EFS
    efs_id                = "${aws_efs_file_system.file_storage.id}"
    efs_mount_directory   = "/var/app/efs"
}
  • Finally, copy .ebextensions/efs_mount.config to your .ebextensions project directory and deploy using AWS EB CLI

Example

  • Take a look at example.tf for an example with Elastic Beanstalk and Route53.
  • Take a look at example_with_cloudfront.tf for an example with Elastic Beanstalk, Cloudfront and Route53.

Customize

Many options are available through variables. Feel free to look into variables.tf inside each module to see all parameters you can setup.

Terraform related documentation

terraform-elastic-beanstalk-nodejs's People

Contributors

basiletrujillo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.