Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_synthetics_canrany: Add environment variables to run config #23574

Merged
merged 13 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
```
27 changes: 25 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,13 @@ 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 {
// get environment variables since they a
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
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 +714,18 @@ func expandCanaryRunConfig(l []interface{}) *synthetics.CanaryRunConfigInput {
codeConfig.ActiveTracing = aws.Bool(v)
}

if vars, ok := m["environment_variables"].(map[string]interface{}); ok {
ev := make(map[string]string)
for k, v := range vars {
ev[k] = v.(string)
}
codeConfig.EnvironmentVariables = aws.StringMap(ev)
}
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved

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 +736,10 @@ func flattenCanaryRunConfig(canaryCodeOut *synthetics.CanaryRunConfigOutput) []i
"active_tracing": aws.BoolValue(canaryCodeOut.ActiveTracing),
}

if v := envVars; v != nil {
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
m["environment_variables"] = aws.StringValueMap(envVars)
}

return []interface{}{m}
}

Expand Down
11 changes: 10 additions & 1 deletion internal/service/synthetics/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,22 @@ func TestAccSyntheticsCanary_run(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "run_config.0.memory_in_mb", "1000"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.timeout_in_seconds", "60"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test1", "result1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"zip_file", "start_canary"},
ImportStateVerifyIgnore: []string{"zip_file", "start_canary", "run_config.0.environment_variables"},
},
{
Config: testAccCanaryRun2Config(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCanaryExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "run_config.0.memory_in_mb", "960"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.timeout_in_seconds", "120"),
resource.TestCheckResourceAttr(resourceName, "run_config.0.environment_variables.test2", "result2"),
),
},
{
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -762,6 +764,9 @@ resource "aws_synthetics_canary" "test" {

run_config {
timeout_in_seconds = 60
environment_variables = {
test1 = "result1"
}
}

depends_on = [aws_iam_role.test, aws_iam_role_policy.test]
Expand All @@ -786,6 +791,10 @@ resource "aws_synthetics_canary" "test" {
run_config {
timeout_in_seconds = 120
memory_in_mb = 960
environment_variables = {
test1 = "result1"
test2 = "result2"
}
}

depends_on = [aws_iam_role.test, aws_iam_role_policy.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