Skip to content

Commit

Permalink
update multi-region
Browse files Browse the repository at this point in the history
  • Loading branch information
coval3nte committed Nov 21, 2021
1 parent 3021714 commit 16a1719
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 47 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ AWS Elastic Beanstalk infrastructure for Sellix's [web-app](https://sellix.io),

### Apply

Initialize Environment
1. Initialize Environment

`export ENV={environment}`

AWS IAM (optional, see main.tf)
2. Initialize TFVARS

`mv terraform.tfvars.example.json terraform.tfvars.json`

then edit

3. Edit Providers according to desired Regions in main.tf
4. AWS IAM (optional, see main.tf)
```
export AWS_ACCESS_KEY=""
export AWS_SECRET_KEY=""
```

Terraform Apply
5. Terraform Apply

```
terraform init
Expand Down
9 changes: 8 additions & 1 deletion beanstalk.tf → beanstalk/beanstalk.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
data "aws_elastic_beanstalk_solution_stack" "nodejs" {
most_recent = true

name_regex = "^64bit Amazon Linux (.*) running Node.js ${var.nodejs_version != null ?
var.nodejs_version : "(.*)"}$"
}

resource "aws_elastic_beanstalk_environment" "sellix-web-app-environment" {
name = local.tags["Project"]
application = aws_elastic_beanstalk_application.sellix-web-app.name
tier = "WebServer"
wait_for_ready_timeout = "20m"
solution_stack_name = "64bit Amazon Linux 2 v5.4.8 running Node.js 12"
solution_stack_name = data.aws_elastic_beanstalk_solution_stack.nodejs.name
setting {
namespace = "aws:elasticbeanstalk:monitoring"
name = "Automatically Terminate Unhealthy Instances"
Expand Down
2 changes: 1 addition & 1 deletion chatbot.tf → beanstalk/chatbot.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data "terraform_remote_state" "sellix-web-app-chatbot-terraform-state" {
}

resource "aws_codestarnotifications_notification_rule" "sellix-web-app-codestarnotifications" {
name = "${local.tags["Project"]}-${local.aws_region}-chatbot"
name = "${local.tags["Project"]}-${var.aws_region}-chatbot"
detail_type = "BASIC"
resource = aws_codepipeline.sellix-web-app-codepipeline.arn
status = "ENABLED"
Expand Down
24 changes: 12 additions & 12 deletions iam.tf → beanstalk/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data "aws_iam_policy_document" "sellix-web-app-elb-policy-document" {
"s3:PutObject",
]
resources = [
"arn:aws:s3:::${local.tags["Project"]}-${local.aws_region}-elb-logs/*"
"arn:aws:s3:::${local.tags["Project"]}-${var.aws_region}-elb-logs/*"
]
principals {
type = "AWS"
Expand Down Expand Up @@ -168,27 +168,27 @@ data "aws_elb_service_account" "sellix-web-app-elb-service" {
}

resource "aws_iam_role" "sellix-web-app-codepipeline-role" {
name = "${local.tags["Project"]}-${local.aws_region}-codepipeline-role"
name = "${local.tags["Project"]}-${var.aws_region}-codepipeline-role"
assume_role_policy = data.aws_iam_policy_document.sellix-web-app-service-policy-document.json
}

resource "aws_iam_role" "sellix-web-app-service-role" {
name = "${local.tags["Project"]}-${local.aws_region}-service-role"
name = "${local.tags["Project"]}-${var.aws_region}-service-role"
assume_role_policy = data.aws_iam_policy_document.sellix-web-app-service-policy-document.json
}

resource "aws_iam_role" "sellix-web-app-ec2-role" {
name = "${local.tags["Project"]}-${local.aws_region}-ec2-role"
name = "${local.tags["Project"]}-${var.aws_region}-ec2-role"
assume_role_policy = data.aws_iam_policy_document.sellix-web-app-ec2-policy-document.json
}

resource "aws_iam_instance_profile" "sellix-web-app-ec2-instance-profile" {
name = "${local.tags["Project"]}-${local.aws_region}-ec2-instance-profile"
name = "${local.tags["Project"]}-${var.aws_region}-ec2-instance-profile"
role = aws_iam_role.sellix-web-app-ec2-role.name
}

resource "aws_iam_role_policy" "sellix-web-app-codepipeline-policy" {
name = "${local.tags["Project"]}-${local.aws_region}-codepipeline-policy"
name = "${local.tags["Project"]}-${var.aws_region}-codepipeline-policy"
role = aws_iam_role.sellix-web-app-codepipeline-role.id
policy = <<EOF
{
Expand Down Expand Up @@ -230,36 +230,36 @@ EOF
}

resource "aws_iam_role_policy" "sellix-web-app-service-sns-policy" {
name = "${local.tags["Project"]}-${local.aws_region}-service-sns-policy"
name = "${local.tags["Project"]}-${var.aws_region}-service-sns-policy"
role = aws_iam_role.sellix-web-app-service-role.id
policy = data.aws_iam_policy_document.sellix-web-app-service-sns-policy-document.json
}

resource "aws_iam_role_policy" "sellix-web-app-default-policy" {
name = "${local.tags["Project"]}-${local.aws_region}-default-policy"
name = "${local.tags["Project"]}-${var.aws_region}-default-policy"
role = aws_iam_role.sellix-web-app-ec2-role.id
policy = data.aws_iam_policy_document.sellix-web-app-default-policy-document.json
}

resource "aws_iam_role" "sellix-web-app-codebuild-role" {
name = "${local.tags["Project"]}-${local.aws_region}-codebuild-role"
name = "${local.tags["Project"]}-${var.aws_region}-codebuild-role"
assume_role_policy = data.aws_iam_policy_document.sellix-web-app-codebuild-assumerole-policy-document.json
}

resource "aws_iam_policy" "sellix-web-app-codebuild-permissions-policy" {
name = "${local.tags["Project"]}-${local.aws_region}-codebuild-permissions-policy"
name = "${local.tags["Project"]}-${var.aws_region}-codebuild-permissions-policy"
path = "/service-role/"
policy = data.aws_iam_policy_document.sellix-web-app-codebuild-permissions-policy-document.json
}

resource "aws_iam_policy" "sellix-web-app-codebuild-policy" {
name = "${local.tags["Project"]}-${local.aws_region}-codebuild-policy"
name = "${local.tags["Project"]}-${var.aws_region}-codebuild-policy"
description = "CodeBuild access policy"
policy = data.aws_iam_policy_document.sellix-web-app-codebuild-policy-document.json
}

resource "aws_iam_policy" "sellix-web-app-codebuild-codestar-connection-policy" {
name = "${local.tags["Project"]}-${local.aws_region}-codebuild-codestar-connection-policy"
name = "${local.tags["Project"]}-${var.aws_region}-codebuild-codestar-connection-policy"
description = "CodeBuild CodeStar Connection policy"
policy = data.aws_iam_policy_document.sellix-web-app-codebuild-codestar-connection-policy-document.json
}
Expand Down
11 changes: 4 additions & 7 deletions locals.tf → beanstalk/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ data "aws_availability_zones" "available" {
}

locals {
workspace_infos = split("$", terraform.workspace)
aws_region = local.workspace_infos[1]
workspace = local.workspace_infos[0]
tags = {
"Project" = "sellix-web-app-v2-${local.workspace}"
"Environment" = local.workspace
"Project" = "sellix-web-app-v2-${terraform.workspace}"
"Environment" = terraform.workspace
}
env = {
ELASTIC_BEANSTALK_PORT = 8080
DOMAIN = local.production ? "sellix.io" : "sellix.gg"
ENVIRONMENT = local.production ? "production" : "staging"
}
production = contains(["prod"], substr(local.workspace, 0, 4)) ? true : false
production = contains(["prod"], substr(terraform.workspace, 0, 4)) ? true : false
notification_topic_arn = { for s in aws_elastic_beanstalk_environment.sellix-web-app-environment.all_settings :
s.name => s.value if s.namespace == "aws:elasticbeanstalk:sns:topics" && s.name == "Notification Topic ARN" }
availability_zones = [data.aws_availability_zones.available.names[0], data.aws_availability_zones.available.names[1]]
Expand Down Expand Up @@ -218,7 +215,7 @@ locals {
{
namespace = "aws:elbv2:listener:443"
name = "SSLCertificateArns"
value = local.production ? var.ssl_arn[local.aws_region]["production"] : var.ssl_arn[local.aws_region]["staging"]
value = local.production ? var.ssl_arn[var.aws_region]["production"] : var.ssl_arn[var.aws_region]["staging"]
},
{
namespace = "aws:elbv2:loadbalancer"
Expand Down
14 changes: 14 additions & 0 deletions beanstalk/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 0.13"
required_providers {
aws = {
"source" = "hashicorp/aws"
}
github = {
source = "integrations/github"
}
random = {
source = "hashicorp/random"
}
}
}
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions s3.tf → beanstalk/s3.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
resource "aws_s3_bucket" "sellix-web-app-codepipeline-s3-bucket" {
bucket = "${local.tags["Project"]}-${local.aws_region}-codepipeline"
bucket = "${local.tags["Project"]}-${var.aws_region}-codepipeline"
acl = "private"
tags = merge({
"Name" = "${local.tags["Project"]}-${local.aws_region}-codepipeline-s3-bucket"
"Name" = "${local.tags["Project"]}-${var.aws_region}-codepipeline-s3-bucket"
},
local.tags
)
}

resource "aws_s3_bucket" "sellix-web-app-elb-logs" {
bucket = "${local.tags["Project"]}-${local.aws_region}-elb-logs"
bucket = "${local.tags["Project"]}-${var.aws_region}-elb-logs"
acl = "private"
policy = join("", data.aws_iam_policy_document.sellix-web-app-elb-policy-document.*.json)
tags = merge({
"Name" = "${local.tags["Project"]}-${local.aws_region}-elb-logs"
"Name" = "${local.tags["Project"]}-${var.aws_region}-elb-logs"
},
local.tags
)
Expand Down
2 changes: 1 addition & 1 deletion security.tf → beanstalk/security.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ resource "aws_security_group" "sellix-web-app-security-group" {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"] # rlly needed?
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge({
"Name" = "${local.tags["Project"]}-security-group"
Expand Down
49 changes: 49 additions & 0 deletions beanstalk/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "aws_key_pair" "sellix-web-app-keypair" {
key_name = "${local.tags["Project"]}-keypair"
public_key = file(var.public_key_path)
lifecycle {
ignore_changes = [public_key]
}
}

variable "aws_access_key" {
default = null
}

variable "aws_secret_key" {
default = null
}

variable "aws_region" {
default = null
}

variable "public_key_path" {
description = "ssh key"
default = "~/.ssh/id_rsa.pub"
}

variable "nodejs_version" {
description = "Beanstalk Node.js Version"
default = null
}

variable "main_cidr_block" {
description = "main cidr"
default = "172.18.0.0/16"
}

variable "github_opts" {
description = "GitHub Repo Name && Organization"
default = {}
}

variable "ssl_arn" {
description = "SSL Certificate ARN"
default = {}
}

variable "codestar_connection_arn" {
description = "CodeStar Connection ARN"
default = null
}
File renamed without changes.
53 changes: 46 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ terraform {
aws = {
"source" = "hashicorp/aws"
}
github = {
source = "integrations/github"
}
random = {
source = "hashicorp/random"
}
}
backend "s3" {
profile = "sellix-terraform"
Expand All @@ -22,8 +16,53 @@ terraform {
}

provider "aws" {
alias = "eu-west-1"
profile = "sellix-terraform"
region = local.aws_region
region = "eu-west-1"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}

provider "aws" {
alias = "us-east-1"
profile = "sellix-terraform"
region = "us-east-1"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}

module "eu-west-1" {
source = "./beanstalk"
providers = {
aws = aws.eu-west-1
}
aws_access_key = var.aws_access_key
aws_secret_key = var.aws_secret_key
nodejs_version = var.nodejs_version
aws_region = "eu-west-1"
github_opts = var.github_opts
ssl_arn = var.ssl_arn
codestar_connection_arn = var.codestar_connection_arn
}

module "us-east-1" {
source = "./beanstalk"
providers = {
aws = aws.us-east-1
}
aws_access_key = var.aws_access_key
aws_secret_key = var.aws_secret_key
aws_region = "us-east-1"
nodejs_version = var.nodejs_version
github_opts = var.github_opts
ssl_arn = var.ssl_arn
codestar_connection_arn = var.codestar_connection_arn
}

output "eu-west-1_eb-cname" {
value = module.eu-west-1.eb_cname
}

output "us-east-1_eb-cname" {
value = module.us-east-1.eb_cname
}
14 changes: 3 additions & 11 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
resource "aws_key_pair" "sellix-web-app-keypair" {
key_name = "${local.tags["Project"]}-keypair"
public_key = file(var.public_key_path)
lifecycle {
ignore_changes = [public_key]
}
}

variable "aws_access_key" {
default = null
}
Expand All @@ -14,9 +6,9 @@ variable "aws_secret_key" {
default = null
}

variable "public_key_path" {
description = "ssh key"
default = "~/.ssh/id_rsa.pub"
variable "nodejs_version" {
description = "Beanstalk Node.js Version"
default = null
}

variable "main_cidr_block" {
Expand Down

0 comments on commit 16a1719

Please sign in to comment.