Skip to content

Commit

Permalink
comment out code deploy resource to support ecs deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
kunduso committed Nov 27, 2024
1 parent 696c75b commit 54367d2
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 173 deletions.
98 changes: 49 additions & 49 deletions deploy/code_deploy.tf
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codedeploy_app
resource "aws_codedeploy_app" "application_main" {
compute_platform = "ECS"
name = var.name
}
# #https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codedeploy_app
# resource "aws_codedeploy_app" "application_main" {
# compute_platform = "ECS"
# name = var.name
# }

#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codedeploy_deployment_group
resource "aws_codedeploy_deployment_group" "application_main" {
app_name = aws_codedeploy_app.application_main.name
deployment_group_name = "${var.name}-deploy-group"
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
service_role_arn = aws_iam_role.codedeploy_role.arn
blue_green_deployment_config {
deployment_ready_option {
action_on_timeout = "CONTINUE_DEPLOYMENT"
}
terminate_blue_instances_on_deployment_success {
action = "TERMINATE"
termination_wait_time_in_minutes = 1
}
}
ecs_service {
cluster_name = local.infra_output["aws_ecs_cluster_name"]
service_name = aws_ecs_service.service.name
}
deployment_style {
deployment_option = "WITH_TRAFFIC_CONTROL"
deployment_type = "BLUE_GREEN"
}
auto_rollback_configuration {
enabled = true
events = ["DEPLOYMENT_FAILURE"]
}
load_balancer_info {
target_group_pair_info {
prod_traffic_route {
listener_arns = [local.infra_output["aws_lb_listener"]]
}
target_group {
name = local.infra_output["aws_lb_blue_target_group_name"]
}
target_group {
name = local.infra_output["aws_lb_green_target_group_name"]
}
}
}
lifecycle {
ignore_changes = [blue_green_deployment_config]
}
}
# #https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codedeploy_deployment_group
# resource "aws_codedeploy_deployment_group" "application_main" {
# app_name = aws_codedeploy_app.application_main.name
# deployment_group_name = "${var.name}-deploy-group"
# deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
# service_role_arn = aws_iam_role.codedeploy_role.arn
# blue_green_deployment_config {
# deployment_ready_option {
# action_on_timeout = "CONTINUE_DEPLOYMENT"
# }
# terminate_blue_instances_on_deployment_success {
# action = "TERMINATE"
# termination_wait_time_in_minutes = 1
# }
# }
# ecs_service {
# cluster_name = local.infra_output["aws_ecs_cluster_name"]
# service_name = aws_ecs_service.service.name
# }
# deployment_style {
# deployment_option = "WITH_TRAFFIC_CONTROL"
# deployment_type = "BLUE_GREEN"
# }
# auto_rollback_configuration {
# enabled = true
# events = ["DEPLOYMENT_FAILURE"]
# }
# load_balancer_info {
# target_group_pair_info {
# prod_traffic_route {
# listener_arns = [local.infra_output["aws_lb_listener"]]
# }
# target_group {
# name = local.infra_output["aws_lb_blue_target_group_name"]
# }
# target_group {
# name = local.infra_output["aws_lb_green_target_group_name"]
# }
# }
# }
# lifecycle {
# ignore_changes = [blue_green_deployment_config]
# }
# }
206 changes: 103 additions & 103 deletions deploy/code_deploy_deployment.tf
Original file line number Diff line number Diff line change
@@ -1,103 +1,103 @@
locals {
# appspec file
appspec = {
version = "0.0"
Resources = [
{
TargetService = {
Type = "AWS::ECS::Service"
Properties = {
TaskDefinition = aws_ecs_task_definition.web_app.arn
LoadBalancerInfo = {
ContainerName = "first"
ContainerPort = 8080
}
}
}
}
]
}

appspec_content = replace(jsonencode(local.appspec), "\"", "\\\"")
appspec_sha256 = sha256(jsonencode(local.appspec))

# create deployment script
script = <<EOF
#!/bin/bash
set -e
echo "Starting CodeDeploy agent deployment"
aws --version
echo "Constructing deployment command..."
COMMAND=$(cat <<EOT
aws deploy create-deployment \
--application-name "${aws_codedeploy_app.application_main.name}" \
--deployment-config-name CodeDeployDefault.ECSAllAtOnce \
--deployment-group-name "${aws_codedeploy_deployment_group.application_main.deployment_group_name}" \
--revision '{"revisionType": "AppSpecContent", "appSpecContent": {"content": "${local.appspec_content}", "sha256":"${local.appspec_sha256}"}}' \
--description "Deployment from Terraform" \
--output json
EOT
)
echo "Command to be executed:"
echo "$COMMAND"
echo "Executing deployment command..."
DEPLOYMENT_INFO=$(eval "$COMMAND")
COMMAND_EXIT_CODE=$?
echo "Command exit code: $COMMAND_EXIT_CODE"
echo "Raw output:"
echo "$DEPLOYMENT_INFO"
if [ $COMMAND_EXIT_CODE -ne 0 ]; then
echo "Error: AWS CLI command failed"
exit $COMMAND_EXIT_CODE
fi
echo "Parsing deployment info..."
if ! DEPLOYMENT_ID=$(echo "$DEPLOYMENT_INFO" | jq -r '.deploymentId'); then
echo "Error: Failed to parse deployment ID from output"
exit 1
fi
if [ "$DEPLOYMENT_ID" == "null" ] || [ -z "$DEPLOYMENT_ID" ]; then
echo "Error: Deployment ID is null or empty"
exit 1
fi
echo "Deployment ID: $DEPLOYMENT_ID"
echo "Deployment created successfully"
EOF
}



#Create the code_deploy.sh file to run the AWS CodeDeploy deployment
#https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file
resource "local_file" "code_deploy_sh" {
content = local.script
filename = "${path.module}/code_deploy.sh"
file_permission = "0755"
depends_on = [
aws_codedeploy_app.application_main,
aws_codedeploy_deployment_group.application_main,
aws_ecs_task_definition.web_app
]
}

#https://developer.hashicorp.com/terraform/language/resources/terraform-data
resource "terraform_data" "trigger_code_deploy_deployment" {
triggers_replace = local_file.code_deploy_sh
provisioner "local-exec" {
command = "./code_deploy.sh"
interpreter = ["/bin/bash", "-c"]
}
depends_on = [local_file.code_deploy_sh]
lifecycle {
replace_triggered_by = [local_file.code_deploy_sh]
}
}
# locals {
# # appspec file
# appspec = {
# version = "0.0"
# Resources = [
# {
# TargetService = {
# Type = "AWS::ECS::Service"
# Properties = {
# TaskDefinition = aws_ecs_task_definition.web_app.arn
# LoadBalancerInfo = {
# ContainerName = "first"
# ContainerPort = 8080
# }
# }
# }
# }
# ]
# }

# appspec_content = replace(jsonencode(local.appspec), "\"", "\\\"")
# appspec_sha256 = sha256(jsonencode(local.appspec))

# # create deployment script
# script = <<EOF
# #!/bin/bash
# set -e

# echo "Starting CodeDeploy agent deployment"
# aws --version

# echo "Constructing deployment command..."
# COMMAND=$(cat <<EOT
# aws deploy create-deployment \
# --application-name "${aws_codedeploy_app.application_main.name}" \
# --deployment-config-name CodeDeployDefault.ECSAllAtOnce \
# --deployment-group-name "${aws_codedeploy_deployment_group.application_main.deployment_group_name}" \
# --revision '{"revisionType": "AppSpecContent", "appSpecContent": {"content": "${local.appspec_content}", "sha256":"${local.appspec_sha256}"}}' \
# --description "Deployment from Terraform" \
# --output json
# EOT
# )

# echo "Command to be executed:"
# echo "$COMMAND"

# echo "Executing deployment command..."
# DEPLOYMENT_INFO=$(eval "$COMMAND")
# COMMAND_EXIT_CODE=$?

# echo "Command exit code: $COMMAND_EXIT_CODE"
# echo "Raw output:"
# echo "$DEPLOYMENT_INFO"

# if [ $COMMAND_EXIT_CODE -ne 0 ]; then
# echo "Error: AWS CLI command failed"
# exit $COMMAND_EXIT_CODE
# fi

# echo "Parsing deployment info..."
# if ! DEPLOYMENT_ID=$(echo "$DEPLOYMENT_INFO" | jq -r '.deploymentId'); then
# echo "Error: Failed to parse deployment ID from output"
# exit 1
# fi

# if [ "$DEPLOYMENT_ID" == "null" ] || [ -z "$DEPLOYMENT_ID" ]; then
# echo "Error: Deployment ID is null or empty"
# exit 1
# fi

# echo "Deployment ID: $DEPLOYMENT_ID"

# echo "Deployment created successfully"
# EOF
# }



# #Create the code_deploy.sh file to run the AWS CodeDeploy deployment
# #https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file
# resource "local_file" "code_deploy_sh" {
# content = local.script
# filename = "${path.module}/code_deploy.sh"
# file_permission = "0755"
# depends_on = [
# aws_codedeploy_app.application_main,
# aws_codedeploy_deployment_group.application_main,
# aws_ecs_task_definition.web_app
# ]
# }

# #https://developer.hashicorp.com/terraform/language/resources/terraform-data
# resource "terraform_data" "trigger_code_deploy_deployment" {
# triggers_replace = local_file.code_deploy_sh
# provisioner "local-exec" {
# command = "./code_deploy.sh"
# interpreter = ["/bin/bash", "-c"]
# }
# depends_on = [local_file.code_deploy_sh]
# lifecycle {
# replace_triggered_by = [local_file.code_deploy_sh]
# }
# }
42 changes: 21 additions & 21 deletions deploy/code_deploy_role.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
resource "aws_iam_role" "codedeploy_role" {
name = "${var.name}-code-deploy-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "codedeploy.amazonaws.com"
}
}
]
})
}
# #https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
# resource "aws_iam_role" "codedeploy_role" {
# name = "${var.name}-code-deploy-role"
# assume_role_policy = jsonencode({
# Version = "2012-10-17"
# Statement = [
# {
# Action = "sts:AssumeRole"
# Effect = "Allow"
# Principal = {
# Service = "codedeploy.amazonaws.com"
# }
# }
# ]
# })
# }

#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
resource "aws_iam_role_policy_attachment" "codedeploy_policy_attachement" {
role = aws_iam_role.codedeploy_role.name
policy_arn = "arn:aws:iam::aws:policy/AWSCodeDeployRoleForECS"
}
# #https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
# resource "aws_iam_role_policy_attachment" "codedeploy_policy_attachement" {
# role = aws_iam_role.codedeploy_role.name
# policy_arn = "arn:aws:iam::aws:policy/AWSCodeDeployRoleForECS"
# }

0 comments on commit 54367d2

Please sign in to comment.