Skip to content

Commit 0b76bbe

Browse files
committed
Added staging terraform
1 parent 482b378 commit 0b76bbe

23 files changed

+1223
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
resource "random_pet" "access_logs" {
2+
length = 3
3+
}
4+
5+
resource "aws_s3_bucket" "access_logs" {
6+
bucket = "${local.env}-access-logs-${random_pet.access_logs.id}"
7+
acl = "private"
8+
force_destroy = true # to make it easier to destroy at this repository example
9+
}
10+
11+
resource "aws_s3_bucket_policy" "access_logs" {
12+
bucket = aws_s3_bucket.access_logs.id
13+
policy = data.aws_iam_policy_document.access_logs.json
14+
}
15+
16+
data "aws_iam_policy_document" "access_logs" {
17+
# Allow from Elastic Load Balancing account in ap-northeast-1
18+
statement {
19+
actions = ["s3:PutObject"]
20+
resources = ["${aws_s3_bucket.access_logs.arn}/*"]
21+
22+
principals {
23+
type = "AWS"
24+
identifiers = ["arn:aws:iam::582318560864:root"]
25+
}
26+
}
27+
}

terraform/stg/alb.tf

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
resource "aws_lb" "public" {
2+
name = "${local.env}-public"
3+
security_groups = [aws_security_group.public.id, aws_security_group.private.id]
4+
subnets = module.vpc.public_subnets
5+
enable_deletion_protection = false # to make it easier to destroy at this repository example
6+
7+
tags = {
8+
Name = "${local.env}-public"
9+
Environment = local.env
10+
}
11+
12+
access_logs {
13+
bucket = aws_s3_bucket.access_logs.bucket
14+
prefix = "ALB/${local.env}-public"
15+
enabled = true
16+
}
17+
}
18+
19+
resource "aws_lb_listener" "public_http" {
20+
load_balancer_arn = aws_lb.public.arn
21+
port = 80
22+
protocol = "HTTP"
23+
24+
default_action {
25+
type = "fixed-response"
26+
27+
fixed_response {
28+
content_type = "text/plain"
29+
message_body = "404 Not Found"
30+
status_code = "404"
31+
}
32+
}
33+
}
34+
35+
resource "aws_lb_target_group" "api_blue" {
36+
name = "${local.env}-api-blue"
37+
port = 3000
38+
protocol = "HTTP"
39+
target_type = "ip"
40+
vpc_id = module.vpc.vpc_id
41+
42+
health_check {
43+
path = "/okcomputer/all"
44+
}
45+
}
46+
47+
resource "aws_lb_target_group" "api_green" {
48+
name = "${local.env}-api-green"
49+
port = 3000
50+
protocol = "HTTP"
51+
target_type = "ip"
52+
vpc_id = module.vpc.vpc_id
53+
54+
health_check {
55+
path = "/okcomputer/all"
56+
}
57+
}
58+
59+
resource "aws_lb_target_group" "web_blue" {
60+
name = "${local.env}-web-blue"
61+
port = 3000
62+
protocol = "HTTP"
63+
target_type = "ip"
64+
vpc_id = module.vpc.vpc_id
65+
66+
health_check {
67+
path = "/okcomputer/all"
68+
}
69+
}
70+
71+
resource "aws_lb_target_group" "web_green" {
72+
name = "${local.env}-web-green"
73+
port = 3000
74+
protocol = "HTTP"
75+
target_type = "ip"
76+
vpc_id = module.vpc.vpc_id
77+
78+
health_check {
79+
path = "/" # TODO: change healthcheck path
80+
}
81+
}
82+
83+
84+
resource "aws_lb_listener_rule" "api" {
85+
listener_arn = aws_lb_listener.public_http.arn
86+
priority = 100
87+
88+
action {
89+
type = "forward"
90+
target_group_arn = aws_lb_target_group.api_blue.arn
91+
}
92+
93+
condition {
94+
path_pattern {
95+
values = ["/api/*"]
96+
}
97+
}
98+
99+
lifecycle {
100+
# Target group will be updated by CodeDeploy
101+
ignore_changes = [action]
102+
}
103+
}
104+
105+
resource "aws_lb_listener_rule" "web" {
106+
listener_arn = aws_lb_listener.public_http.arn
107+
priority = 200
108+
109+
action {
110+
type = "forward"
111+
target_group_arn = aws_lb_target_group.web_blue.arn
112+
}
113+
114+
condition {
115+
path_pattern {
116+
values = ["/*"]
117+
}
118+
}
119+
120+
lifecycle {
121+
# Target group will be updated by CodeDeploy
122+
ignore_changes = [action]
123+
}
124+
}

terraform/stg/codebuild.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module "codebuild_iam" {
2+
source = "../modules/codebuild_iam"
3+
name = "${local.env}-codebuild-service-role"
4+
subnet_arns = module.vpc.private_subnet_arns
5+
assets_bucket_arn = data.terraform_remote_state.common.outputs.assets_bucket.arn
6+
codepipeline_artifacts_bucket_arn = aws_s3_bucket.codepipeline.arn
7+
codebuild_bucket_arn = aws_s3_bucket.codebuild.arn
8+
}
9+
10+
resource "random_pet" "codebuild" {
11+
length = 3
12+
}
13+
14+
resource "aws_s3_bucket" "codebuild" {
15+
bucket = "${local.env}-codebuild-${random_pet.codebuild.id}"
16+
acl = "private"
17+
}
18+
19+
resource "aws_s3_bucket_object" "buildspec" {
20+
bucket = aws_s3_bucket.codebuild.id
21+
key = "${local.env}/buildspec.yaml"
22+
content = templatefile("${path.module}/templates/buildspec.yaml", {
23+
repository_domain = dirname(data.terraform_remote_state.common.outputs.ecr_rails_blog_example_repository_url)
24+
repository_url = data.terraform_remote_state.common.outputs.ecr_rails_blog_example_repository_url
25+
bucket = aws_s3_bucket.codebuild.id
26+
env = local.env
27+
database_url = aws_ssm_parameter.database_url.name
28+
secret_key_base = aws_ssm_parameter.secret_key_base.name
29+
asset_bucket = aws_ssm_parameter.asset_bucket.name
30+
})
31+
}
32+
33+
resource "aws_codebuild_project" "build" {
34+
name = "${local.env}-build"
35+
service_role = module.codebuild_iam.service_role_arn
36+
37+
artifacts {
38+
type = "NO_ARTIFACTS"
39+
}
40+
41+
environment {
42+
compute_type = "BUILD_GENERAL1_SMALL"
43+
image = "aws/codebuild/amazonlinux2-x86_64-standard:2.0"
44+
type = "LINUX_CONTAINER"
45+
image_pull_credentials_type = "CODEBUILD"
46+
privileged_mode = true
47+
}
48+
49+
source {
50+
type = "GITHUB"
51+
location = data.github_repository.repo.http_clone_url
52+
git_clone_depth = 1
53+
buildspec = "${aws_s3_bucket.codebuild.arn}/${aws_s3_bucket_object.buildspec.key}"
54+
}
55+
56+
cache {
57+
type = "LOCAL"
58+
modes = ["LOCAL_DOCKER_LAYER_CACHE", "LOCAL_SOURCE_CACHE"]
59+
}
60+
61+
vpc_config {
62+
vpc_id = module.vpc.vpc_id
63+
subnets = module.vpc.private_subnets
64+
security_group_ids = [aws_security_group.private.id, aws_security_group.db.id]
65+
}
66+
}

terraform/stg/codedeploy.tf

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
module "codedeploy_iam" {
2+
source = "../modules/codedeploy_iam"
3+
name = "${local.env}-codedeploy-service-role"
4+
}
5+
6+
resource "aws_codedeploy_app" "app" {
7+
name = "${local.env}-app"
8+
compute_platform = "ECS"
9+
}
10+
11+
# Deployment for API service
12+
13+
resource "aws_codedeploy_deployment_group" "api" {
14+
app_name = aws_codedeploy_app.app.name
15+
deployment_group_name = "${local.env}-api"
16+
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
17+
service_role_arn = module.codedeploy_iam.service_role_arn
18+
19+
blue_green_deployment_config {
20+
deployment_ready_option {
21+
action_on_timeout = "CONTINUE_DEPLOYMENT"
22+
}
23+
24+
terminate_blue_instances_on_deployment_success {
25+
action = "TERMINATE"
26+
termination_wait_time_in_minutes = 0
27+
}
28+
}
29+
30+
deployment_style {
31+
deployment_option = "WITH_TRAFFIC_CONTROL"
32+
deployment_type = "BLUE_GREEN"
33+
}
34+
35+
ecs_service {
36+
cluster_name = aws_ecs_cluster.cluster.name
37+
service_name = aws_ecs_service.api.name
38+
}
39+
40+
load_balancer_info {
41+
target_group_pair_info {
42+
prod_traffic_route {
43+
listener_arns = [aws_lb_listener.public_http.arn]
44+
}
45+
46+
target_group {
47+
name = aws_lb_target_group.api_blue.name
48+
}
49+
50+
target_group {
51+
name = aws_lb_target_group.api_green.name
52+
}
53+
}
54+
}
55+
}
56+
57+
# Deployment for Web service
58+
59+
resource "aws_codedeploy_deployment_group" "web" {
60+
app_name = aws_codedeploy_app.app.name
61+
deployment_group_name = "${local.env}-web"
62+
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
63+
service_role_arn = module.codedeploy_iam.service_role_arn
64+
65+
blue_green_deployment_config {
66+
deployment_ready_option {
67+
action_on_timeout = "CONTINUE_DEPLOYMENT"
68+
}
69+
70+
terminate_blue_instances_on_deployment_success {
71+
action = "TERMINATE"
72+
termination_wait_time_in_minutes = 0
73+
}
74+
}
75+
76+
deployment_style {
77+
deployment_option = "WITH_TRAFFIC_CONTROL"
78+
deployment_type = "BLUE_GREEN"
79+
}
80+
81+
ecs_service {
82+
cluster_name = aws_ecs_cluster.cluster.name
83+
service_name = aws_ecs_service.web.name
84+
}
85+
86+
load_balancer_info {
87+
target_group_pair_info {
88+
prod_traffic_route {
89+
listener_arns = [aws_lb_listener.public_http.arn]
90+
}
91+
92+
target_group {
93+
name = aws_lb_target_group.web_blue.name
94+
}
95+
96+
target_group {
97+
name = aws_lb_target_group.web_green.name
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)