Skip to content

Commit

Permalink
Merge pull request #23574 from hashicorp/r/aws_synthetics_carany_envi…
Browse files Browse the repository at this point in the history
…ronment_variables

resource/aws_synthetics_canrany: Add environment variables to run config
  • Loading branch information
johnsonaj authored Mar 9, 2022
2 parents 653ac6e + cc1e9db commit 8715267
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/23574.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_synthetics_canary: Add optional `environment_variables` to `run_config`.
```
22 changes: 20 additions & 2 deletions internal/service/synthetics/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func ResourceCanary() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"environment_variables": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"memory_in_mb": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -385,7 +390,12 @@ func resourceCanaryRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting vpc config: %w", err)
}

if err := d.Set("run_config", flattenCanaryRunConfig(canary.RunConfig)); err != nil {
runConfig := &synthetics.CanaryRunConfigInput{}
if v, ok := d.GetOk("run_config"); ok {
runConfig = expandCanaryRunConfig(v.([]interface{}))
}

if err := d.Set("run_config", flattenCanaryRunConfig(canary.RunConfig, runConfig.EnvironmentVariables)); err != nil {
return fmt.Errorf("error setting run config: %w", err)
}

Expand Down Expand Up @@ -703,10 +713,14 @@ func expandCanaryRunConfig(l []interface{}) *synthetics.CanaryRunConfigInput {
codeConfig.ActiveTracing = aws.Bool(v)
}

if vars, ok := m["environment_variables"].(map[string]interface{}); ok && len(vars) > 0 {
codeConfig.EnvironmentVariables = flex.ExpandStringMap(vars)
}

return codeConfig
}

func flattenCanaryRunConfig(canaryCodeOut *synthetics.CanaryRunConfigOutput) []interface{} {
func flattenCanaryRunConfig(canaryCodeOut *synthetics.CanaryRunConfigOutput, envVars map[string]*string) []interface{} {
if canaryCodeOut == nil {
return []interface{}{}
}
Expand All @@ -717,6 +731,10 @@ func flattenCanaryRunConfig(canaryCodeOut *synthetics.CanaryRunConfigOutput) []i
"active_tracing": aws.BoolValue(canaryCodeOut.ActiveTracing),
}

if envVars != nil {
m["environment_variables"] = aws.StringValueMap(envVars)
}

return []interface{}{m}
}

Expand Down
94 changes: 94 additions & 0 deletions internal/service/synthetics/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,49 @@ func TestAccSyntheticsCanary_runTracing(t *testing.T) {
})
}

func TestAccSyntheticsCanary_runEnvironmentVariables(t *testing.T) {
var conf synthetics.Canary
rName := fmt.Sprintf("tf-acc-test-%s", sdkacctest.RandString(8))
resourceName := "aws_synthetics_canary.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, synthetics.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckCanaryDestroy,
Steps: []resource.TestStep{
{
Config: testAccCanaryRunEnvVariables1Config(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test1", "result1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "run_config.0.environment_variables"},
},
{
Config: testAccCanaryRunEnvVariables2Config(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test1", "result1"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test2", "result2"),
),
},
{
Config: testAccCanaryBasicConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckNoResourceAttr(resourceName, "run_config.0.environment_variables"),
),
},
},
})
}

func TestAccSyntheticsCanary_vpc(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test in short mode")
Expand Down Expand Up @@ -817,6 +860,57 @@ resource "aws_synthetics_canary" "test" {
`, rName, tracing))
}

func testAccCanaryRunEnvVariables1Config(rName string) string {
return acctest.ConfigCompose(testAccCanaryBaseConfig(rName), fmt.Sprintf(`
resource "aws_synthetics_canary" "test" {
name = %[1]q
artifact_s3_location = "s3://${aws_s3_bucket.test.bucket}/"
execution_role_arn = aws_iam_role.test.arn
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
schedule {
expression = "rate(0 minute)"
}
run_config {
environment_variables = {
test1 = "result1"
}
}
depends_on = [aws_iam_role.test, aws_iam_role_policy.test]
}
`, rName))
}

func testAccCanaryRunEnvVariables2Config(rName string) string {
return acctest.ConfigCompose(testAccCanaryBaseConfig(rName), fmt.Sprintf(`
resource "aws_synthetics_canary" "test" {
name = %[1]q
artifact_s3_location = "s3://${aws_s3_bucket.test.bucket}/"
execution_role_arn = aws_iam_role.test.arn
handler = "exports.handler"
zip_file = "test-fixtures/lambdatest.zip"
runtime_version = "syn-nodejs-puppeteer-3.2"
schedule {
expression = "rate(0 minute)"
}
run_config {
environment_variables = {
test1 = "result1"
test2 = "result2"
}
}
depends_on = [aws_iam_role.test, aws_iam_role_policy.test]
}
`, rName))
}

func testAccCanaryBasicConfig(rName string) string {
return acctest.ConfigCompose(testAccCanaryBaseConfig(rName), fmt.Sprintf(`
resource "aws_synthetics_canary" "test" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/synthetics_canary.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ The following arguments are optional:
* `timeout_in_seconds` - (Optional) Number of seconds the canary is allowed to run before it must stop. If you omit this field, the frequency of the canary is used, up to a maximum of 840 (14 minutes).
* `memory_in_mb` - (Optional) Maximum amount of memory available to the canary while it is running, in MB. The value you specify must be a multiple of 64.
* `active_tracing` - (Optional) Whether this canary is to use active AWS X-Ray tracing when it runs. You can enable active tracing only for canaries that use version syn-nodejs-2.0 or later for their canary runtime.
* `environment_variables` - (Optional) Map of environment variables that are accessible from the canary during execution. Please see [AWS Docs](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime) for variables reserved for Lambda.

### vpc_config

Expand Down

0 comments on commit 8715267

Please sign in to comment.