Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 282] AWS Code pipeline demo improvement v1 #283

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions getting-started/cicd/codepipeline/modules/codedeploy/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
resource "aws_codedeploy_app" "demo_codepipeline" {
compute_platform = "Server"
name = "aws_codedeploy_app_demo"
name = var.codedeploy_app_name
}

resource "aws_codedeploy_deployment_config" "demo_codepipeline" {
deployment_config_name = "test-deployment-config"
deployment_config_name = var.deployment_config_name

minimum_healthy_hosts {
type = "HOST_COUNT"
Expand All @@ -15,7 +15,7 @@ resource "aws_codedeploy_deployment_config" "demo_codepipeline" {

resource "aws_codedeploy_deployment_group" "demo_codepipeline" {
app_name = aws_codedeploy_app.demo_codepipeline.name
deployment_group_name = "example-group"
deployment_group_name = var.deployment_group_name
service_role_arn = var.service_role_arn

deployment_config_name = aws_codedeploy_deployment_config.demo_codepipeline.id
Expand All @@ -29,16 +29,10 @@ resource "aws_codedeploy_deployment_group" "demo_codepipeline" {
ec2_tag_filter {
type = "KEY_AND_VALUE"
key = "Name"
value = "MyCodePipelineDemo"
value = var.ec2_tag_filter_name
}
}

# trigger_configuration {
# trigger_events = ["DeploymentFailure"]
# trigger_name = "example-trigger"
# trigger_target_arn = aws_sns_topic.example.arn
# }

auto_rollback_configuration {
enabled = true
events = ["DEPLOYMENT_FAILURE"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,27 @@ variable "service_role_arn" {
description = "ARN of the IAM profile"
type = string
}

variable "deployment_group_name" {
description = "Name of the deployment group"
type = string
default = "demo-deployment-group"
}

variable "deployment_config_name" {
description = "Name of the deployment configuration"
type = string
default = "demo-deployment-config"
}

variable "codedeploy_app_name" {
description = "Name of the codedeploy app"
type = string
default = "demo-codedeploy-app"
}

variable "ec2_tag_filter_name" {
description = "Name of the EC2 tag"
type = string
default = "MyCodePipelineDemo"
}
27 changes: 9 additions & 18 deletions getting-started/cicd/codepipeline/modules/codepipeline/main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

resource "aws_codepipeline" "codepipeline" {
name = "tf-test-pipeline"
name = var.codepipeline_name
role_arn = aws_iam_role.codepipeline_role.arn

artifact_store {
location = var.s3_bucket_id
type = "S3"

}

stage {
Expand All @@ -21,8 +20,8 @@ resource "aws_codepipeline" "codepipeline" {
output_artifacts = ["source_output"]

configuration = {
ConnectionArn = aws_codestarconnections_connection.example.arn
FullRepositoryId = "tungbq/aws-codepipeline-demo"
ConnectionArn = aws_codestarconnections_connection.demo_codepipeline.arn
FullRepositoryId = var.github_repo_name
BranchName = "main"
}
}
Expand All @@ -32,33 +31,25 @@ resource "aws_codepipeline" "codepipeline" {
name = "Deploy"

action {
name = "MyDemoApplicationStage"
name = "ApplicationDeployment"
category = "Deploy"
owner = "AWS"
provider = "CodeDeploy"
version = "1"
input_artifacts = ["source_output"]
configuration = {
ApplicationName = "aws_codedeploy_app_demo"
DeploymentGroupName = "example-group"
DeploymentGroupName = var.deployment_group_name
}
}
}
}

resource "aws_codestarconnections_connection" "example" {
name = "example-connection"
resource "aws_codestarconnections_connection" "demo_codepipeline" {
name = var.aws_codestarconnections_connection_name
provider_type = "GitHub"
}

data "aws_secretsmanager_secret" "my_secret" {
name = "prod/github/tungb" # Replace with your secret name
}

data "aws_secretsmanager_secret_version" "my_secret_version" {
secret_id = data.aws_secretsmanager_secret.my_secret.id
}

data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
Expand All @@ -73,7 +64,7 @@ data "aws_iam_policy_document" "assume_role" {
}

resource "aws_iam_role" "codepipeline_role" {
name = "test-role"
name = var.aws_iam_role_name
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}

Expand All @@ -100,7 +91,7 @@ data "aws_iam_policy_document" "codepipeline_policy" {
statement {
effect = "Allow"
actions = ["codestar-connections:UseConnection"]
resources = [aws_codestarconnections_connection.example.arn]
resources = [aws_codestarconnections_connection.demo_codepipeline.arn]
}

statement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,38 @@ variable "s3_bucket_id" {
description = "ID of the S3 bucket"
type = string
}

variable "s3_bucket_arn" {
description = "The ARN for the S3 bucket"
type = string
}

variable "codepipeline_name" {
description = "Code pipeline name"
type = string
default = "tf_demo_codepipeline"
}

variable "github_repo_name" {
description = "Github repository naming"
type = string
default = "tungbq/aws-codepipeline-demo"
}

variable "aws_codestarconnections_connection_name" {
description = "Codestar connection naming"
type = string
default = "demo-codepipeline-connection"
}

variable "deployment_group_name" {
description = "Name of the deployment group"
type = string
default = "demo-deployment-group"
}

variable "aws_iam_role_name" {
description = "Name of the deployment group"
type = string
default = "demo-codepipeline-role"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# Launch EC2 with codedeploy agent service up and running
resource "aws_instance" "basic_ec2_instance" {
# To get the AMI ID, visit: https://us-east-1.console.aws.amazon.com/ec2/home?region=us-east-1#AMICatalog
ami = "ami-0f34c5ae932e6f0e4"
Expand All @@ -9,12 +9,12 @@ resource "aws_instance" "basic_ec2_instance" {

user_data = file("${path.module}/scripts/install_codedeploy.sh")
tags = {
Name = "MyCodePipelineDemo"
Name = var.ec2_tag_name
}
}

resource "aws_security_group" "ec2_codedeploy" {
name = "ec2-codedeploy-security-group"
name = var.ec2_codedeploy_sg_name
description = "Focalboard security group allowing ports 22 and 80"

ingress {
Expand Down Expand Up @@ -46,21 +46,3 @@ resource "aws_security_group" "ec2_codedeploy" {
ipv6_cidr_blocks = ["::/0"]
}
}

# resource "null_resource" "install_codedeploy" {
# provisioner "remote-exec" {
# inline = [
# "chmod +x /path/to/your/install_codedeploy_agent.sh",
# "/path/to/your/install_codedeploy_agent.sh"
# ]

# connection {
# type = "ssh"
# user = "ec2-user" # or your SSH user
# private_key = file("/path/to/your/private_key.pem")
# host = aws_instance.example.public_ip # or your instance's public IP
# }
# }

# depends_on = [aws_instance.basic_ec2_instance]
# }
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ variable "profile_name" {
description = "Name of the IAM profile"
type = string
}

variable "ec2_tag_name" {
description = "Name of the EC2 tag"
type = string
default = "MyCodePipelineDemo"
}

variable "ec2_codedeploy_sg_name" {
description = "ec2-codedeploy-security-group"
type = string
default = "ec2-codedeploy-security-group"
}
3 changes: 1 addition & 2 deletions getting-started/cicd/codepipeline/modules/iam/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

resource "aws_iam_role" "instance_role" {
name = "EC2InstanceRole"
assume_role_policy = <<EOF
Expand Down Expand Up @@ -35,7 +34,7 @@ output "profile_name" {
}

resource "aws_iam_instance_profile" "ec2_role_for_codebuild" {
name = "ec2_role_for_codebuild"
name = var.ec2_role_for_codebuild_name
role = aws_iam_role.instance_role.name
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# modules/s3_bucket/variables.tf
variable "ec2_role_for_codebuild_name" {
description = "ec2_role_for_codebuild_name"
type = string
default = "ec2_role_for_codebuild"
}