Skip to content

Commit 98b3bce

Browse files
committed
Refactor to allow individual tf modules
address #25 (comment)
1 parent 9ad201f commit 98b3bce

30 files changed

+211
-12
lines changed

cloud_environments/dev/module.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module main {
2-
source = "../../cloud_module"
2+
source = "../../cloud_module/pipeline"
33
environment_name = var.environment_name
44
project_alias = var.project_alias
55
slack_signing_secret = var.slack_signing_secret
66
slack_post_webhook_url = var.slack_post_webhook_url
7+
repo_dir = var.repo_dir
78
}

cloud_environments/dev/module_variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ variable environment_name {
1818
default = ""
1919
description = "Empty string for Production, otherwise the environment name e.g. dev, stage, etc, make sure to use lowercase (s3 bucket only allows lower)"
2020
}
21+
22+
variable repo_dir {
23+
type = string
24+
description = "The absolute path of git repository path"
25+
}

cloud_environments/dev_table/.terraform.lock.hcl

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "iriversland-cloud"
4+
key = "terraform/media-literacy-dev--table.remote-terraform.tfstate"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform init -backend-config=local.backend.credentials.tfvars
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main_table {
2+
source = "${var.repo_dir}/cloud_module/dynamodb"
3+
environment_name = var.environment_name
4+
project_alias = var.project_alias
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "project_alias" {
2+
type = string
3+
description = "Name prefix used for step function and related resources, including the domain name, so please only use [0-9a-z_-]"
4+
}
5+
6+
variable environment_name {
7+
type = string
8+
default = ""
9+
description = "Empty string for Production, otherwise the environment name e.g. dev, stage, etc, make sure to use lowercase (s3 bucket only allows lower)"
10+
}
11+
12+
variable repo_dir {
13+
type = string
14+
description = "The absolute path of git repository path"
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set -e
2+
3+
REPO_DIR=$(git rev-parse --show-toplevel)
4+
ENV=dev_table sh "${REPO_DIR}/cloud_environments/terraform.sh" "$@"
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module main {
2-
source = "../../cloud_module"
2+
source = "../../cloud_module/pipeline"
33
environment_name = var.environment_name
44
project_alias = var.project_alias
55
slack_signing_secret = var.slack_signing_secret
66
slack_post_webhook_url = var.slack_post_webhook_url
7+
repo_dir = var.repo_dir
78
}

cloud_environments/production/module_variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ variable environment_name {
1818
default = ""
1919
description = "Empty string for Production, otherwise the environment name e.g. dev, stage, etc, make sure to use lowercase (s3 bucket only allows lower)"
2020
}
21+
22+
variable repo_dir {
23+
type = string
24+
description = "The absolute path of git repository path"
25+
}

cloud_environments/terraform.sh

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
set -e
22

3-
DEPLOY_DIR=$(git rev-parse --show-toplevel)/cloud_environments/${ENV:-production}
4-
GOLANG_SRC_DIR=$(git rev-parse --show-toplevel)/lambda_golang
5-
PYTHON_SRC_DIR=$(git rev-parse --show-toplevel)/lambda
3+
REPO_DIR=$(git rev-parse --show-toplevel)
4+
DEPLOY_DIR=${REPO_DIR}/cloud_environments/${ENV:-production}
5+
GOLANG_SRC_DIR=${REPO_DIR}/lambda_golang
6+
PYTHON_SRC_DIR=${REPO_DIR}/lambda
67

78
set -o allexport
89
. ${DEPLOY_DIR}/local.backend.credentials.tfvars
@@ -14,6 +15,7 @@ TF_VAR_project_alias=media-literacy
1415
TF_VAR_environment_name=${ENV:-}
1516
TF_VAR_slack_signing_secret=${slack_signing_secret}
1617
TF_VAR_slack_post_webhook_url=${slack_post_webhook_url}
18+
TF_VAR_repo_dir=${REPO_DIR}
1719
set +o allexport
1820

1921
# below is just for testing golang build! The actual build command is in terraform lambda module `command` property

cloud_module/dynamodb/table.tf

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
resource "aws_dynamodb_table" "basic-dynamodb-table" {
2+
name = "GameScores"
3+
billing_mode = "PROVISIONED"
4+
read_capacity = 20
5+
write_capacity = 20
6+
hash_key = "UserId"
7+
range_key = "GameTitle"
8+
9+
attribute {
10+
name = "UserId"
11+
type = "S"
12+
}
13+
14+
attribute {
15+
name = "GameTitle"
16+
type = "S"
17+
}
18+
19+
attribute {
20+
name = "TopScore"
21+
type = "N"
22+
}
23+
24+
ttl {
25+
attribute_name = "TimeToExist"
26+
enabled = false
27+
}
28+
29+
global_secondary_index {
30+
name = "GameTitleIndex"
31+
hash_key = "GameTitle"
32+
range_key = "TopScore"
33+
write_capacity = 10
34+
read_capacity = 10
35+
projection_type = "INCLUDE"
36+
non_key_attributes = ["UserId"]
37+
}
38+
39+
tags = {
40+
Name = "dynamodb-table-1"
41+
Environment = "production"
42+
}
43+
}

cloud_module/dynamodb/variables.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "project_alias" {
2+
type = string
3+
description = "Name prefix used for step function and related resources, including the domain name, so please only use [0-9a-z_-]"
4+
}
5+
6+
variable environment_name {
7+
type = string
8+
default = ""
9+
description = "Empty string for Production, otherwise the environment name e.g. dev, stage, etc, make sure to use lowercase (s3 bucket only allows lower)"
10+
}
11+
12+
locals {
13+
project_name = var.environment_name != "" ? "${var.project_alias}-${var.environment_name}" : "${var.project_alias}"
14+
environment = var.environment_name != "" ? var.environment_name : "prod_table"
15+
}

cloud_module/dynamodb/version.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0.2" # minimum version that supports M1
3+
4+
required_providers {
5+
# please use env var to pass over credentials
6+
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs
7+
aws = ">= 4.9.0" # minimum version based on `terraform init` error message
8+
}
9+
}
File renamed without changes.

cloud_module/api.tf cloud_module/pipeline/api.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module "slack_command_lambda" {
5656
description = "Lambda function for slack command for environment ${local.project_name}"
5757
handler = "slack_command_controller.lambda_handler"
5858
runtime = "python3.8"
59-
source_path = "${path.module}/../lambda/src/slack_command_controller.py"
59+
source_path = "${var.repo_dir}/lambda/src/slack_command_controller.py"
6060

6161
layers = [
6262
module.lambda_layer.lambda_layer_arn
File renamed without changes.
File renamed without changes.
File renamed without changes.

cloud_module/lambda.tf cloud_module/pipeline/lambda.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module "lambda_layer" {
1010
runtime = "python3.8"
1111
compatible_runtimes = ["python3.8"]
1212
source_path = [{
13-
path = "${path.module}/../lambda/layer"
13+
path = "${var.repo_dir}/lambda/layer"
1414
pip_requirements = true
1515
# Make sure the follow the Layer Structure
1616
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
@@ -67,7 +67,7 @@ module "scraper_lambda" {
6767
# Based on tf https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/master/examples/build-package/main.tf#L111
6868
# Based on golang https://github.com/snsinfu/terraform-lambda-example/blob/master/Makefile#L23
6969
source_path = [{
70-
path = "${path.module}/../lambda_golang/"
70+
path = "${var.repo_dir}/lambda_golang/"
7171
commands = ["${local.go_build_flags} go build ./cmd/landing", ":zip"]
7272
patterns = ["landing"]
7373
}]
File renamed without changes.

cloud_module/queue.tf cloud_module/pipeline/queue.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module "pipeline_queue_consumer_lambda" {
2929
description = "Consumer lambda function for ${local.project_name} pipeline queue"
3030
handler = "pipeline_queue_consumer.lambda_handler"
3131
runtime = "python3.8"
32-
source_path = "${path.module}/../lambda/src/pipeline_queue_consumer.py"
32+
source_path = "${var.repo_dir}/lambda/src/pipeline_queue_consumer.py"
3333
publish = true
3434

3535
layers = [
File renamed without changes.

cloud_module/stories_sfn.tf cloud_module/pipeline/stories_sfn.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module landing_parse_metadata_lambda {
3636
runtime = "go1.x"
3737

3838
source_path = [{
39-
path = "${path.module}/../lambda_golang/"
39+
path = "${var.repo_dir}/lambda_golang/"
4040
commands = ["${local.go_build_flags} go build ./cmd/landing_metadata", ":zip"]
4141
patterns = ["landing_metadata"]
4242
}]
@@ -101,7 +101,7 @@ module fetch_story_lambda {
101101
runtime = "go1.x"
102102

103103
source_path = [{
104-
path = "${path.module}/../lambda_golang/"
104+
path = "${var.repo_dir}/lambda_golang/"
105105
commands = ["${local.go_build_flags} go build ./cmd/story", ":zip"]
106106
patterns = ["story"]
107107
}]

cloud_module/stories_sqs.tf cloud_module/pipeline/stories_sqs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module "stories_queue_consumer_lambda" {
3434
handler = "stories"
3535
runtime = "go1.x"
3636
source_path = [{
37-
path = "${path.module}/../lambda_golang/"
37+
path = "${var.repo_dir}/lambda_golang/"
3838
commands = ["${local.go_build_flags} go build ./cmd/stories", ":zip"]
3939
patterns = ["stories"]
4040
}]

cloud_module/variables.tf cloud_module/pipeline/variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ variable environment_name {
1919
description = "Empty string for Production, otherwise the environment name e.g. dev, stage, etc, make sure to use lowercase (s3 bucket only allows lower)"
2020
}
2121

22+
variable repo_dir {
23+
type = string
24+
description = "The absolute path of git repository path"
25+
}
26+
2227
locals {
2328
project_name = var.environment_name != "" ? "${var.project_alias}-${var.environment_name}" : "${var.project_alias}"
2429
environment = var.environment_name != "" ? var.environment_name : "prod"
File renamed without changes.

0 commit comments

Comments
 (0)