|
| 1 | +resource "random_id" "id" { |
| 2 | + byte_length = 8 |
| 3 | +} |
| 4 | + |
| 5 | +resource "aws_s3_bucket" "codepipeline_artifacts" { |
| 6 | + bucket = "codepipeline-artifacts-${random_id.id.hex}" |
| 7 | + |
| 8 | + tags = { |
| 9 | + Name = "CodePipelineArtifactsBucket" |
| 10 | + Environment = "DevSecOps" |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +resource "aws_secretsmanager_secret" "github_token" { |
| 15 | + name = "github-oauth-token" |
| 16 | + description = "GitHub OAuth token for CodePipeline access" |
| 17 | +} |
| 18 | + |
| 19 | +resource "aws_secretsmanager_secret_version" "github_token" { |
| 20 | + secret_id = aws_secretsmanager_secret.github_token.id |
| 21 | + secret_string = "your-personal-access-token" # Replace this with the GitHub Personal Access Token |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_iam_role" "codepipeline_role" { |
| 25 | + name = "${var.resource_prefix}-pipeline-role" |
| 26 | + |
| 27 | + assume_role_policy = jsonencode({ |
| 28 | + Version = "2012-10-17" |
| 29 | + Statement = [ |
| 30 | + { |
| 31 | + Action = "sts:AssumeRole" |
| 32 | + Effect = "Allow" |
| 33 | + Principal = { |
| 34 | + Service = "codepipeline.amazonaws.com" |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_iam_policy" "codepipeline_policy" { |
| 42 | + name = "CodePipelinePolicy-${random_id.id.hex}" |
| 43 | + description = "Policy for CodePipeline" |
| 44 | + |
| 45 | + policy = jsonencode({ |
| 46 | + Version = "2012-10-17" |
| 47 | + Statement = [ |
| 48 | + { |
| 49 | + Effect = "Allow" |
| 50 | + Action = [ |
| 51 | + "s3:*", |
| 52 | + "codebuild:*", |
| 53 | + "iam:PassRole", |
| 54 | + "secretsmanager:GetSecretValue" |
| 55 | + ] |
| 56 | + Resource = "*" |
| 57 | + } |
| 58 | + ] |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +resource "aws_iam_role_policy_attachment" "codepipeline_policy_attach" { |
| 63 | + role = aws_iam_role.codepipeline_role.name |
| 64 | + policy_arn = aws_iam_policy.codepipeline_policy.arn |
| 65 | +} |
| 66 | + |
| 67 | +resource "aws_codepipeline" "pipeline" { |
| 68 | + name = "${var.resource_prefix}-devsecops-pipeline" |
| 69 | + role_arn = aws_iam_role.codepipeline_role.arn |
| 70 | + |
| 71 | + artifact_store { |
| 72 | + type = "S3" |
| 73 | + location = aws_s3_bucket.codepipeline_artifacts.id |
| 74 | + } |
| 75 | + |
| 76 | + stage { |
| 77 | + name = "Source" |
| 78 | + |
| 79 | + action { |
| 80 | + name = "Source" |
| 81 | + category = "Source" |
| 82 | + owner = "ThirdParty" |
| 83 | + provider = "GitHub" |
| 84 | + version = "1" |
| 85 | + output_artifacts = ["SourceArtifact"] |
| 86 | + |
| 87 | + configuration = { |
| 88 | + Owner = "your-github-username" |
| 89 | + Repo = "your-repository-name" |
| 90 | + Branch = "main" |
| 91 | + OAuthToken = aws_secretsmanager_secret_version.github_token.secret_string |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + stage { |
| 98 | + name = "Build" |
| 99 | + |
| 100 | + action { |
| 101 | + name = "Build" |
| 102 | + category = "Build" |
| 103 | + owner = "AWS" |
| 104 | + provider = "CodeBuild" |
| 105 | + version = "1" |
| 106 | + input_artifacts = ["SourceArtifact"] |
| 107 | + output_artifacts = ["BuildArtifact"] |
| 108 | + |
| 109 | + configuration = { |
| 110 | + ProjectName = aws_codebuild_project.build_project.name |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + stage { |
| 116 | + name = "Scan" |
| 117 | + |
| 118 | + action { |
| 119 | + name = "StaticCodeAnalysis" |
| 120 | + category = "Test" |
| 121 | + owner = "AWS" |
| 122 | + provider = "CodeBuild" |
| 123 | + version = "1" |
| 124 | + input_artifacts = ["BuildArtifact"] |
| 125 | + |
| 126 | + configuration = { |
| 127 | + ProjectName = aws_codebuild_project.static_analysis_project.name |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +resource "aws_codebuild_project" "build_project" { |
| 134 | + name = "${var.resource_prefix}-build-prj" |
| 135 | + service_role = aws_iam_role.codepipeline_role.arn |
| 136 | + |
| 137 | + environment { |
| 138 | + compute_type = "BUILD_GENERAL1_SMALL" |
| 139 | + image = "aws/codebuild/standard:5.0" |
| 140 | + type = "LINUX_CONTAINER" |
| 141 | + privileged_mode = true |
| 142 | + } |
| 143 | + |
| 144 | + source { |
| 145 | + type = "NO_SOURCE" |
| 146 | + buildspec = file("./buildspecs/buildproject.yml") |
| 147 | + } |
| 148 | + |
| 149 | + artifacts { |
| 150 | + type = "CODEPIPELINE" |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +resource "aws_codebuild_project" "static_analysis_project" { |
| 155 | + name = "${var.resource_prefix}-sast-scanning-prj" |
| 156 | + service_role = aws_iam_role.codepipeline_role.arn |
| 157 | + |
| 158 | + environment { |
| 159 | + compute_type = "BUILD_GENERAL1_SMALL" |
| 160 | + image = "aws/codebuild/standard:5.0" |
| 161 | + type = "LINUX_CONTAINER" |
| 162 | + privileged_mode = true |
| 163 | + } |
| 164 | + |
| 165 | + source { |
| 166 | + type = "NO_SOURCE" |
| 167 | + buildspec = file("./buildspecs/sastscanning.yml") |
| 168 | + } |
| 169 | + |
| 170 | + artifacts { |
| 171 | + type = "CODEPIPELINE" |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +resource "aws_codebuild_project" "oss_scanning_project" { |
| 176 | + name = "${var.resource_prefix}-oss-scanning-prj" |
| 177 | + service_role = aws_iam_role.codepipeline_role.arn |
| 178 | + |
| 179 | + environment { |
| 180 | + compute_type = "BUILD_GENERAL1_SMALL" |
| 181 | + image = "aws/codebuild/standard:5.0" |
| 182 | + type = "LINUX_CONTAINER" |
| 183 | + privileged_mode = true |
| 184 | + } |
| 185 | + |
| 186 | + source { |
| 187 | + type = "NO_SOURCE" |
| 188 | + buildspec = file("./buildspecs/ossdepscan.yml") |
| 189 | + } |
| 190 | + |
| 191 | + artifacts { |
| 192 | + type = "CODEPIPELINE" |
| 193 | + } |
| 194 | +} |
0 commit comments