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

improvement: apply best practice for getting-started/cicd/codebuild terraform code #279

Merged
merged 1 commit into from
Dec 9, 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
19 changes: 0 additions & 19 deletions getting-started/cicd/codebuild/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 20 additions & 96 deletions getting-started/cicd/codebuild/main.tf
Original file line number Diff line number Diff line change
@@ -1,115 +1,39 @@
# Set AWS Provider
provider "aws" {
region = "us-east-1" # Replace with your desired region
}

# Create S3 buckets
resource "aws_s3_bucket" "demo_aws_codebuild_bucket_output" {
bucket = "tungbq-demo-aws-codebuild-bucket-output"
# Module: S3 Bucket Creation
module "s3_bucket" {
source = "./modules/s3_bucket"

bucket_name = "tungbq-demo-aws-codebuild-bucket-output"
tags = {
Name = "S3 bucket to store output code"
Environment = "Dev"
}

force_destroy = true
}

# Module: IAM Role Creation
module "iam_role" {
source = "./modules/iam_role"


data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"

principals {
type = "Service"
identifiers = ["codebuild.amazonaws.com"]
}

actions = ["sts:AssumeRole"]
}
}

resource "aws_iam_role" "demo_codebuild" {
name = "demo_codebuild"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
service_name = "codebuild.amazonaws.com"
}

data "aws_iam_policy_document" "demo_codebuild" {
statement {
effect = "Allow"

actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]

resources = ["*"]
}

statement {
effect = "Allow"

actions = [
"ec2:CreateNetworkInterface",
"ec2:DescribeDhcpOptions",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups",
"ec2:DescribeVpcs",
]

resources = ["*"]
}

# Module: IAM Role Policy Creation
module "iam_role_policy" {
source = "./modules/iam_role_policy"

statement {
effect = "Allow"
actions = ["s3:*"]
resources = [
aws_s3_bucket.demo_aws_codebuild_bucket_output.arn,
"${aws_s3_bucket.demo_aws_codebuild_bucket_output.arn}/*",
]
}
}

resource "aws_iam_role_policy" "demo_codebuild" {
role = aws_iam_role.demo_codebuild.name
policy = data.aws_iam_policy_document.demo_codebuild.json
role_name = module.iam_role.role_name
s3_bucket_arn = module.s3_bucket.bucket_arn
}

# Module: CodeBuild Project Creation
module "codebuild_project" {
source = "./modules/codebuild_project"

### CODE BUILD PROJECT
resource "aws_codebuild_project" "demo_project" {
name = "demo_project"
description = "Demo project"
build_timeout = 5
queued_timeout = 5

service_role = aws_iam_role.demo_codebuild.arn

artifacts {
type = "S3"
location = aws_s3_bucket.demo_aws_codebuild_bucket_output.id
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/amazonlinux2-x86_64-standard:4.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
}

source {
type = "GITHUB"
location = "https://github.com/tungbq/aws-cicd-source-example.git"
git_clone_depth = 1
}

source_version = "main"

tags = {
Environment = "Test"
}
project_name = "demo_project"
service_role = module.iam_role.role_arn
s3_bucket_id = module.s3_bucket.bucket_id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# modules/codebuild_project/main.tf
resource "aws_codebuild_project" "demo_project" {
name = var.project_name
description = "Demo project"
build_timeout = 5
queued_timeout = 5

service_role = var.service_role

artifacts {
type = "S3"
location = var.s3_bucket_id
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/amazonlinux2-x86_64-standard:4.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
}

source {
type = "GITHUB"
location = "https://github.com/tungbq/aws-cicd-source-example.git"
git_clone_depth = 1
}

source_version = "main"

tags = {
Environment = "Test"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# modules/codebuild_project/variables.tf
variable "project_name" {
description = "Name of the CodeBuild project"
type = string
}

variable "service_role" {
description = "ARN of the service role for CodeBuild"
type = string
}

variable "s3_bucket_id" {
description = "ID of the S3 bucket"
type = string
}
27 changes: 27 additions & 0 deletions getting-started/cicd/codebuild/modules/iam_role/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# modules/iam_role/main.tf
resource "aws_iam_role" "demo_codebuild" {
name = var.service_name

assume_role_policy = data.aws_iam_policy_document.assume_role.json
}

data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"

principals {
type = "Service"
identifiers = [var.service_name]
}

actions = ["sts:AssumeRole"]
}
}

output "role_arn" {
value = aws_iam_role.demo_codebuild.arn
}

output "role_name" {
value = aws_iam_role.demo_codebuild.name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# modules/iam_role/variables.tf
variable "service_name" {
description = "Service name for IAM role"
type = string
}
41 changes: 41 additions & 0 deletions getting-started/cicd/codebuild/modules/iam_role_policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# modules/iam_role_policy/main.tf
resource "aws_iam_role_policy" "demo_codebuild" {
role = var.role_name
policy = data.aws_iam_policy_document.demo_codebuild.json
}

data "aws_iam_policy_document" "demo_codebuild" {
statement {
effect = "Allow"

actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]

resources = ["*"]
}

statement {
effect = "Allow"

actions = [
"ec2:CreateNetworkInterface",
"ec2:DescribeDhcpOptions",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups",
"ec2:DescribeVpcs",
]

resources = ["*"]
}

statement {
effect = "Allow"
actions = ["s3:*"]
resources = [var.s3_bucket_arn, "${var.s3_bucket_arn}/*"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# modules/iam_role_policy/variables.tf
variable "role_name" {
description = "Name of the IAM role"
type = string
}

variable "s3_bucket_arn" {
description = "ARN of the S3 bucket"
type = string
}
16 changes: 16 additions & 0 deletions getting-started/cicd/codebuild/modules/s3_bucket/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# modules/s3_bucket/main.tf
resource "aws_s3_bucket" "demo_aws_codebuild_bucket_output" {
bucket = var.bucket_name

tags = var.tags

force_destroy = true
}

output "bucket_arn" {
value = aws_s3_bucket.demo_aws_codebuild_bucket_output.arn
}

output "bucket_id" {
value = aws_s3_bucket.demo_aws_codebuild_bucket_output.id
}
10 changes: 10 additions & 0 deletions getting-started/cicd/codebuild/modules/s3_bucket/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# modules/s3_bucket/variables.tf
variable "bucket_name" {
description = "The name for the S3 bucket"
type = string
}

variable "tags" {
description = "Tags for the S3 bucket"
type = map(string)
}