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-270: Terraform getting started with AWS CodeBuild #277

Merged
merged 12 commits into from
Dec 9, 2023
43 changes: 43 additions & 0 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.

115 changes: 115 additions & 0 deletions getting-started/cicd/codebuild/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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"

tags = {
Name = "S3 bucket to store output code"
Environment = "Dev"
}

force_destroy = true
}



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
}

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 = [
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
}


### 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"
}
}