-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamically configurable post deployment stages (#100)
Co-authored-by: Moritz Zimmer <[email protected]>
- Loading branch information
1 parent
8fd2cf7
commit 1280a19
Showing
13 changed files
with
278 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
locals { | ||
codebuild_name = "custom-pipeline-step" | ||
} | ||
|
||
resource "aws_cloudwatch_log_group" "custom_step" { | ||
name = "/aws/codebuild/${local.codebuild_name}" | ||
retention_in_days = 1 | ||
} | ||
|
||
resource "aws_codebuild_project" "custom_step" { | ||
name = local.codebuild_name | ||
service_role = aws_iam_role.custom_codepipeline_step.arn | ||
|
||
artifacts { | ||
type = "CODEPIPELINE" | ||
} | ||
|
||
logs_config { | ||
cloudwatch_logs { | ||
group_name = aws_cloudwatch_log_group.custom_step.name | ||
status = "ENABLED" | ||
} | ||
|
||
s3_logs { | ||
status = "DISABLED" | ||
} | ||
} | ||
|
||
environment { | ||
compute_type = "BUILD_GENERAL1_SMALL" | ||
image = "aws/codebuild/amazonlinux2-x86_64-standard:5.0" | ||
type = "LINUX_CONTAINER" | ||
} | ||
|
||
source { | ||
type = "CODEPIPELINE" | ||
buildspec = templatefile("${path.module}/codepipeline_step/buildspec.yml", { script = file("${path.module}/codepipeline_step/step.py") }) | ||
} | ||
} | ||
|
||
data "aws_iam_policy_document" "custom_codepipeline_step" { | ||
statement { | ||
sid = "Logging" | ||
|
||
actions = [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents" | ||
] | ||
|
||
resources = ["arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/codebuild/*"] | ||
} | ||
|
||
# Required for code build to access the code pipeline artifact bucket | ||
# to be modified depending on input artifacts | ||
statement { | ||
sid = "S3BuildArtifactAccess" | ||
|
||
actions = [ | ||
"s3:GetObject", | ||
"s3:GetObjectVersion" | ||
] | ||
|
||
#tfsec:ignore:aws-iam-no-policy-wildcards | ||
resources = ["${module.deployment.codepipeline_artifact_storage_arn}/deploy/*"] | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "custom_codepipeline_step" { | ||
name = "${local.codebuild_name}-${data.aws_region.current.name}" | ||
policy = data.aws_iam_policy_document.custom_codepipeline_step.json | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "custom_codepipeline_step" { | ||
role = aws_iam_role.custom_codepipeline_step.name | ||
policy_arn = aws_iam_policy.custom_codepipeline_step.arn | ||
} | ||
|
||
resource "aws_iam_role" "custom_codepipeline_step" { | ||
name = "${local.codebuild_name}-${data.aws_region.current.name}" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Action = "sts:AssumeRole" | ||
Effect = "Allow" | ||
Sid = "" | ||
Principal = { | ||
Service = "codebuild.amazonaws.com" | ||
} | ||
}, | ||
] | ||
}) | ||
} | ||
|
||
# Required for code pipeline to execute custom code build steps | ||
data "aws_iam_policy_document" "codepipeline_execution" { | ||
statement { | ||
sid = "CustomCodeBuild" | ||
|
||
actions = [ | ||
"codebuild:BatchGetBuilds", | ||
"codebuild:StartBuild", | ||
] | ||
|
||
resources = [aws_codebuild_project.custom_step.arn] | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "codepipeline_execution" { | ||
name = "allow-${local.codebuild_name}-${data.aws_region.current.name}" | ||
policy = data.aws_iam_policy_document.codepipeline_execution.json | ||
} | ||
|
||
resource "aws_iam_policy_attachment" "codepipeline_execution" { | ||
name = "allow-${local.codebuild_name}-${data.aws_region.current.name}" | ||
policy_arn = aws_iam_policy.codepipeline_execution.arn | ||
roles = [module.deployment.codepipeline_role_name] | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/deployment/complete/codepipeline_step/buildspec.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: 0.2 | ||
phases: | ||
install: | ||
on-failure: ABORT | ||
runtime-versions: | ||
python: 3.11 | ||
build: | ||
commands: | ||
- | | ||
cat << BUILD > function.py | ||
${indent(8, script)} | ||
BUILD | ||
- python function.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
|
||
print("Hello world from CodePipeline stage: " + os.environ.get("FOO", "default value")) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.