Skip to content

Commit f656b03

Browse files
Initial
0 parents  commit f656b03

22 files changed

+1506
-0
lines changed

.terraform.lock.hcl

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Infra

main.tf

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
locals {
2+
env_namespace = join("_", [var.org_name, var.team_name, var.project_id, var.env["dev"]])
3+
general_namespace = join("_", [var.org_name, var.team_name, var.project_id])
4+
s3_bucket_namespace = join("-", [var.org_name, var.team_name, var.project_id, var.env["dev"]])
5+
}
6+
data "aws_caller_identity" "current" {}
7+
module "codepipeline" {
8+
source = "./modules/codepipeline"
9+
general_namespace = local.general_namespace
10+
env_namespace = local.env_namespace
11+
s3_bucket_namespace = local.s3_bucket_namespace
12+
codecommit_repo = module.codecommit.codecommit_configs.repository_name
13+
codecommit_branch = module.codecommit.codecommit_configs.default_branch
14+
codebuild_image = var.codebuild_image
15+
codebuild_type = var.codebuild_type
16+
codebuild_compute_type = var.codebuild_compute_type
17+
ecr_repo_arn = module.ecr.ecr_configs.ecr_repo_arn
18+
build_args = [
19+
{
20+
name = "REPO_URI"
21+
value = module.ecr.ecr_configs.ecr_repo_url
22+
},
23+
{
24+
name = "REPO_ARN"
25+
value = module.ecr.ecr_configs.ecr_repo_arn
26+
},
27+
{
28+
name = "TERRAFORM_VERSION"
29+
value = var.terraform_ver
30+
},
31+
{
32+
name = "ENV_NAMESPACE"
33+
value = local.env_namespace
34+
},
35+
{
36+
name = "AWS_ACCOUNT_ID"
37+
value = data.aws_caller_identity.current.account_id
38+
}
39+
]
40+
}
41+
42+
module "codecommit" {
43+
source = "./modules/codecommit"
44+
general_namespace = local.general_namespace
45+
env_namespace = local.env_namespace
46+
codecommit_branch = var.codecommit_branch
47+
}
48+
49+
module "ecr" {
50+
source = "./modules/ecr"
51+
general_namespace = local.general_namespace
52+
env_namespace = local.env_namespace
53+
}

modules/codecommit/main.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
data "aws_caller_identity" "current" {}
3+
resource "aws_codecommit_repository" "codecommit_repo" {
4+
repository_name = "${var.general_namespace}_code_repo"
5+
default_branch = "${var.codecommit_branch}"
6+
description = "Application repo for lambda ${var.general_namespace}"
7+
}

modules/codecommit/outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "codecommit_configs" {
2+
value = {
3+
repository_name = aws_codecommit_repository.codecommit_repo.repository_name
4+
default_branch = aws_codecommit_repository.codecommit_repo.default_branch
5+
clone_repository_url = aws_codecommit_repository.codecommit_repo.clone_url_http
6+
}
7+
}

modules/codecommit/variables.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
variable "codecommit_branch" {
2+
type = string
3+
default = "master"
4+
}
5+
variable "general_namespace" {
6+
type = string
7+
}
8+
variable "env_namespace" {
9+
type = string
10+
}

modules/codepipeline/main.tf

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
locals {
2+
projects = ["build", "scan", "deploy"]
3+
}
4+
resource "aws_s3_bucket" "codepipeline_bucket" {
5+
bucket = "${var.s3_bucket_namespace}-codepipeline-bucket"
6+
}
7+
8+
resource "aws_s3_bucket_acl" "codepipeline_bucket_acl" {
9+
bucket = aws_s3_bucket.codepipeline_bucket.id
10+
acl = "private"
11+
}
12+
13+
resource "aws_codebuild_project" "project" {
14+
count = length(local.projects)
15+
name = "${var.env_namespace}_${local.projects[count.index]}"
16+
#name = "${var.org}_${var.name}_${var.attribute}_${var.env["dev"]}_codebuild_docker_build"
17+
build_timeout = "5" #The default is 60 minutes.
18+
service_role = aws_iam_role.lambda_codebuild_role.arn
19+
artifacts {
20+
type = "CODEPIPELINE"
21+
}
22+
environment {
23+
compute_type = var.codebuild_compute_type
24+
image = var.codebuild_image
25+
type = var.codebuild_type
26+
#compute_type = "BUILD_GENERAL1_MEDIUM"
27+
#image = "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
28+
#type = "LINUX_CONTAINER"
29+
image_pull_credentials_type = "CODEBUILD"
30+
privileged_mode = true
31+
32+
dynamic "environment_variable" {
33+
for_each = var.build_args
34+
content {
35+
name = environment_variable.value.name
36+
value = environment_variable.value.value
37+
}
38+
}
39+
}
40+
source {
41+
type = "CODEPIPELINE"
42+
buildspec = file("${path.module}/templates/buildspec_${local.projects[count.index]}.yml")
43+
#buildspec = file("${path.module}/stage1-buildspec.yml")
44+
}
45+
46+
source_version = "master"
47+
48+
tags = {
49+
env = var.env_namespace
50+
}
51+
}
52+
53+
resource "aws_codepipeline" "codepipeline" {
54+
name = "${var.env_namespace}_pipeline"
55+
role_arn = aws_iam_role.lambda_codepipeline_role.arn
56+
57+
artifact_store {
58+
location = aws_s3_bucket.codepipeline_bucket.bucket
59+
type = "S3"
60+
}
61+
62+
stage {
63+
name = "Source"
64+
65+
action {
66+
name = "Source"
67+
category = "Source"
68+
owner = "AWS"
69+
provider = "CodeCommit"
70+
version = "1"
71+
output_artifacts = [
72+
"source_output"]
73+
74+
configuration = {
75+
#BranchName = aws_codecommit_repository.lambda_codecommit_repo.default_branch
76+
BranchName = var.codecommit_branch
77+
RepositoryName = var.codecommit_repo
78+
#RepositoryName = aws_codecommit_repository.lambda_codecommit_repo.repository_name
79+
}
80+
}
81+
}
82+
83+
stage {
84+
name = "Build"
85+
86+
action {
87+
name = "Docker_Build"
88+
category = "Build"
89+
owner = "AWS"
90+
provider = "CodeBuild"
91+
input_artifacts = [
92+
"source_output"]
93+
version = "1"
94+
95+
configuration = {
96+
ProjectName = aws_codebuild_project.project[0].name
97+
}
98+
}
99+
}
100+
stage {
101+
name = "Scan"
102+
103+
action {
104+
name = "Sonar_Scan"
105+
category = "Build"
106+
owner = "AWS"
107+
provider = "CodeBuild"
108+
input_artifacts = [
109+
"source_output"]
110+
version = "1"
111+
112+
configuration = {
113+
ProjectName = aws_codebuild_project.project[1].name
114+
}
115+
}
116+
}
117+
stage {
118+
name = "Deploy"
119+
120+
action {
121+
name = "Deploy_Docker"
122+
category = "Build"
123+
owner = "AWS"
124+
provider = "CodeBuild"
125+
input_artifacts = [
126+
"source_output"]
127+
version = "1"
128+
129+
configuration = {
130+
ProjectName = aws_codebuild_project.project[2].name
131+
}
132+
}
133+
}
134+
}

modules/codepipeline/outputs.tf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output "codepipeline_configs" {
2+
value = {
3+
codepipeline = aws_codepipeline.codepipeline.arn
4+
}
5+
}
6+
output "deployment_role_arn" {
7+
value = aws_iam_role.lambda_codebuild_role.arn
8+
}

0 commit comments

Comments
 (0)