Skip to content

Commit 229eb0b

Browse files
author
Shaked Braimok Yosef
committed
added required resources for codedeploy as part of codepipeline
0 parents  commit 229eb0b

4 files changed

Lines changed: 235 additions & 0 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# terraform-aws-codedeploy [![Senora](https://github.com/Senora-dev/assets/blob/main/senora-logo.png?raw=true)](https://senora.dev)
2+
A Terraform module that implements Blue-Green deployment using AWS CodeDeploy as part of a AWS CodePipeline pipeline.
3+
4+
Learn more in the [AWS CodeDeploy Guides Series](https://medium.com/@senora-dev).
5+
6+
## Usage
7+
```terraform
8+
module "codedeploy"{
9+
source = "Senora-dev/codedeploy/aws"
10+
version = "~>1.0.0"
11+
12+
app_name = "my-app-name"
13+
s3_bucket = "codepipeline-bucket-name"
14+
ecs_cluster_name = "my-app-cluster-name"
15+
ecs_cluster_name = "my-app-ecs-sevrice-name"
16+
load_balancer_listener_arn = "${data.my_alb_listener_arn}"
17+
load_balancer_test_listener_arn = "${data.my_test_listener_arn}"
18+
load_balancer_blue_target_group = "my-blue-tg-name"
19+
load_balancer_green_target_group = "my-green-tg-name"
20+
}
21+
```
22+
23+
## Contributing
24+
Contributions to this project are welcome! Feel free to submit issues, feature requests, or pull requests to help improve the self-service backend.
25+
26+
## License
27+
This project is licensed under the [Apache 2.0 License](LICENSE).

data.tf

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
data "aws_s3_bucket" "codepipeline_bucket" {
2+
bucket = var.s3_bucket
3+
}
4+
data "aws_iam_policy_document" "codedeploy_role_policy" {
5+
statement {
6+
actions = [
7+
"ecs:DescribeServices",
8+
"ecs:CreateTaskSet",
9+
"ecs:UpdateServicePrimaryTaskSet",
10+
"ecs:DeleteTaskSet",
11+
]
12+
resources = [data.ecs]
13+
}
14+
statement {
15+
actions = ["cloudwatch:DescribeAlarms"]
16+
resources = ["*"]
17+
}
18+
statement {
19+
actions = ["sns:Publish"]
20+
resources = ["arn:aws:sns:*:*:CodeDeployTopic_*"]
21+
}
22+
statement {
23+
actions = [
24+
"elasticloadbalancing:DescribeTargetGroups",
25+
"elasticloadbalancing:DescribeListeners",
26+
"elasticloadbalancing:ModifyListener",
27+
"elasticloadbalancing:DescribeRules",
28+
"elasticloadbalancing:ModifyRule"
29+
]
30+
resources = [var.alb_listener_arn,var.alb_test_listener_arn]
31+
}
32+
statement {
33+
actions = ["lambda:InvokeFunction"]
34+
resources = ["arn:aws:lambda:*:*:function:CodeDeployHook_*"]
35+
}
36+
statement {
37+
actions = [
38+
"s3:GetObject",
39+
"s3:GetObjectVersion",
40+
"s3:GetBucketVersioning",
41+
"s3:PutObjectAcl",
42+
"s3:PutObject"
43+
]
44+
resources = [
45+
"${data.aws_s3_bucket.codepipeline_bucket.arn}",
46+
"${data.aws_s3_bucket.codepipeline_bucket.arn}/*"
47+
]
48+
}
49+
#statement {
50+
# actions = ["iam:PassRole"]
51+
# resources = var.ecs_iam_roles_arns
52+
#}
53+
54+
#statement {
55+
# actions = [
56+
# "codedeploy:*"
57+
# ]
58+
# resources = ["*"]
59+
#}
60+
}

main.tf

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
resource "aws_codedeploy_app" "codedeploy_app" {
2+
name = "codedeploy-${var.app_name}"
3+
compute_platform = "ECS"
4+
}
5+
6+
7+
resource "aws_codedeploy_deployment_group" "deployment_group" {
8+
app_name = aws_codedeploy_app.codedeploy_app.name
9+
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
10+
deployment_group_name = "ecs-deployment-group-${var.app_name}"
11+
service_role_arn = aws_iam_role.codedeploy_role.arn
12+
13+
auto_rollback_configuration {
14+
enabled = true
15+
events = ["DEPLOYMENT_FAILURE"]
16+
}
17+
18+
blue_green_deployment_config {
19+
deployment_ready_option {
20+
action_on_timeout = "CONTINUE_DEPLOYMENT"
21+
}
22+
23+
terminate_blue_instances_on_deployment_success {
24+
action = "TERMINATE"
25+
termination_wait_time_in_minutes = var.termination_wait_time_in_minutes
26+
}
27+
}
28+
29+
deployment_style {
30+
deployment_option = "WITH_TRAFFIC_CONTROL"
31+
deployment_type = "BLUE_GREEN"
32+
}
33+
34+
ecs_service {
35+
cluster_name = var.ecs_cluster_name
36+
service_name = var.ecs_service_name
37+
}
38+
39+
load_balancer_info {
40+
target_group_pair_info {
41+
prod_traffic_route {
42+
listener_arns = [var.load_balancer_listener_arn]
43+
}
44+
45+
test_traffic_route {
46+
listener_arns = [var.load_balancer_test_listener_arn]
47+
}
48+
49+
target_group {
50+
name = var.load_balancer_blue_target_group
51+
}
52+
53+
target_group {
54+
name = var.load_balancer_green_target_group
55+
}
56+
}
57+
}
58+
}
59+
60+
resource "aws_iam_role" "codedeploy_role" {
61+
name = "iam-role-codedeploy-${var.app_name}"
62+
63+
assume_role_policy = jsonencode({
64+
Version = "2012-10-17"
65+
Statement = [
66+
{
67+
Action = "sts:AssumeRole"
68+
Effect = "Allow"
69+
Sid = ""
70+
Principal = {
71+
Service = "codedeploy.amazonaws.com"
72+
}
73+
},
74+
]
75+
})
76+
}
77+
78+
resource "aws_iam_role_policy" "cloudWatch_policy" {
79+
name = "iam-policy-cloudwatch-${var.app_name}"
80+
role = aws_iam_role.codedeploy_role.id
81+
82+
# Terraform's "jsonencode" function converts a
83+
# Terraform expression result to valid JSON syntax.
84+
policy = jsonencode({
85+
Version = "2012-10-17"
86+
Statement = [
87+
{
88+
Action = [
89+
"logs:CreateLogGroup",
90+
"logs:CreateLogStream",
91+
"logs:PutLogEvents"
92+
]
93+
Effect = "Allow"
94+
Resource = "*"
95+
},
96+
]
97+
})
98+
}
99+
100+
resource "aws_iam_role_policy" "ecs_policy" {
101+
name = "iam-policy-ecs-${var.app_name}"
102+
role = aws_iam_role.codedeploy_role.id
103+
policy = data.aws_iam_policy_document.codedeploy_role_policy.json
104+
}
105+
106+
resource "aws_iam_role_policy_attachment" "role-lambda-execution" {
107+
role = "${aws_iam_role.codedeploy_role.name}"
108+
policy_arn = "arn:aws:iam::aws:policy/AWSLambda_FullAccess"
109+
}

variables.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
variable "name" {
2+
type = string
3+
}
4+
5+
variable "s3_bucket" {
6+
type = string
7+
}
8+
9+
variable "ecs_cluster_name" {
10+
type = string
11+
}
12+
13+
variable "ecs_service_name" {
14+
type = string
15+
}
16+
17+
variable "alb_listener_arn" {
18+
type = string
19+
}
20+
21+
variable "alb_test_listener_arn" {
22+
type = string
23+
}
24+
25+
variable "alb_tg_blue_name" {
26+
type = string
27+
}
28+
29+
variable "alb_tg_green_name" {
30+
type = string
31+
}
32+
33+
variable "ecs_iam_roles_arns" {
34+
type = list(string)
35+
}
36+
37+
variable "termination_wait_time_in_minutes" {
38+
default = 120
39+
}

0 commit comments

Comments
 (0)