-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathmain.tf
110 lines (93 loc) · 2.45 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
terraform {
required_version = ">= 1.4"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.0.0"
}
null = {
source = "hashicorp/null"
version = ">= 3.2.0"
}
}
}
//========================================
locals {
repo_name = "stocks"
region = "us-west-2"
}
//========================================
provider "aws" {
region = local.region
}
//========================================
resource "aws_ecr_repository" "stocks-api" {
name = "stocks-api"
force_delete = true
}
resource "aws_ecr_repository" "stocks-app" {
name = "stocks-app"
force_delete = true
}
resource "aws_ecr_repository" "stocks-db" {
name = "stocks-db"
force_delete = true
}
//========================================
module "stocks_cicd" {
source = "./modules/cicd"
repo_name = local.repo_name
repo_default_branch = "main"
region = local.region
build_timeout = "5"
build_compute_type = "BUILD_GENERAL1_SMALL"
build_image = "aws/codebuild/amazonlinux2-x86_64-standard:4.0"
build_privileged_override = true
buildspec_stocks_api = "./stocks-api/buildspec.yml"
buildspec_stocks_app = "./stocks-app/buildspec.yml"
buildspec_stocks_db = "./stocks-db/buildspec.yml"
force_artifact_destroy = true
}
resource "aws_iam_role_policy" "codebuild_policy" {
name = "serverless-codebuild-automation-policy"
role = module.stocks_cicd.codebuild_role_name
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:CompleteLayerUpload",
"ecr:GetAuthorizationToken",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
],
"Resource": "*",
"Effect": "Allow"
}
]
}
POLICY
}
//========================================
resource "null_resource" "stocks_source_code" {
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = path.module
command = <<EOT
echo loading source code...
cd ./stocks
git init
git remote add -f ${local.repo_name} https://git-codecommit.${local.region}.amazonaws.com/v1/repos/${local.repo_name}
git checkout -b main
git add .
git commit -m "initial commit"
git push --set-upstream ${local.repo_name} main
EOT
}
depends_on = [
module.stocks_cicd
]
}