With the feature update released last night it seems you are now able to use the same CodeBuild project for more builds by having CodePipeline pass environment vars through meaning you can use the same build across environments, for example.
Announcement here: https://aws.amazon.com/about-aws/whats-new/2019/10/aws-codepipeline-enables-setting-environment-variables-on-aws-codebuild-build-jobs/
Action reference here: https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodeBuild.html
Updated CodeBuild example here: https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodeBuild.html#action-reference-CodeBuild-example
I tried my example below on the off chance it worked but received this error:
Error: module.frontend_code_services.aws_codepipeline.this: stage.1.action.0.configuration (EnvironmentVariables): '' expected type 'string', got unconvertible type '[]interface {}'
Potentially this means all configuration options are defined, or if it is just using whatever is allowed in AWS then there's a check to ensure it's a string instead of allowing it to check when called.
resource "aws_codepipeline" "this" {
name = "${var.codecommit_name}-pipeline"
role_arn = var.codepipeline_role_arn
artifact_store {
[..]
}
stage {
name = "Source"
[..]
}
stage {
name = "Build"
action {
name = "BuildFrontend"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source"]
output_artifacts = [
[..]
]
version = "1"
configuration = {
ProjectName = aws_codebuild_project.this.name
# NEW CONFIGURATION
EnvironmentVariables = [
{
name = "Application"
value = "Frontend"
type = "PLAINTEXT"
},
]
}
}
action {
name = "BuildBackend"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source"]
output_artifacts = [
[..]
]
version = "1"
configuration = {
ProjectName = aws_codebuild_project.this.name
# NEW CONFIGURATION
EnvironmentVariables = [
{
name = "Application"
value = "Backend"
type = "PLAINTEXT"
},
]
}
}
}
stage {
name = "DeployDevelopment"
action {
[..]
}
action {
[..]
}
}
None
This works with the jsonencode function:
EnvironmentVariables = jsonencode([
{
name = "TEST_VARIABLE"
value = "test_value"
type = "PLAINTEXT"
}
])
@cretueusebiu ah, interesting, thank you - that helps with a short-term goal.
Do we think that will be _the_ way to do it or is it reasonable to expect to be able to implement this directly in Terraform without the function?
@dannyleesmith It would be nice to have it like the environment_variable from aws_codebuild_project.
One thing I've noticed though, is that there is a limit on the AWS part, where the length of the EnvironmentVariables string can't be more than 1000.
+1
Do we think that will be the way to do it or is it reasonable to expect to be able to implement this directly in Terraform without the function?
@dannyleesmith, One thing to keep in mind is that the AWS Action Reference itself is expecting a string (hence jsonencode working). This is different from codebuild create_project where environment variables are actually a json array. I'm guessing the aws code team leveraged the string/string limited configuration object as a quick win.
Keeping the string in the configuration object provides benefits:
configuration translation from terraform to aws definition within terraform-provider-aws.one could add a friendly terraform interface outside the configuration field and then have terraform-provider-aws code marshal it to a string. however then there are two competing fields for environment variables. 🤷♂
Hi folks 👋 As mentioned above, this appears to be supported by the Terraform resource already.
One thing to keep in mind here is that the configuration argument is a map of string keys to string values, which is not something we can change without deprecating the existing argument and introducing another. The maintainers would also prefer to follow suit with the API's implementation, which itself is already the map implementation, to not introduce our own maintenance burden for new or potentially buggy functionality.
In the original issue post, the Error: module.frontend_code_services.aws_codepipeline.this: stage.1.action.0.configuration (EnvironmentVariables): '' expected type 'string', got unconvertible type '[]interface {}' appears to be type-mismatch messaging from Terraform 0.11 and earlier. Terraform 0.12 and later should provide a similar, but hopefully a little friendlier error message. The recommendation of using jsonencode() in Terraform 0.12 should be helpful as well.
Since there does not appear to be anything actionable in this issue other than potentially some documentation or example updates, I'm going to optimistically close this. If you feel the documentation is lacking, please feel free to submit a new GitHub issue or review/edit the resource documentation source in the website/docs/r/codepipeline.html.markdown file in this repository. Thanks. 😄
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!
Most helpful comment
This works with the jsonencode function: