Skip to content

Commit

Permalink
Merge pull request #28871 from niallthomson/f-aws_apprunner_service-r…
Browse files Browse the repository at this point in the history
…untimesecrets

r/aws_apprunner_service: Support for runtime_environment_secrets
  • Loading branch information
ewbankkit authored Jan 13, 2023
2 parents 310a46c + ad3f8e2 commit 950252e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/28871.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_apprunner_service: Add `source_configuration.code_repository.code_configuration.runtime_environment_secrets` and `source_configuration.image_repository.image_configuration.runtime_environment_secrets` argument
```
26 changes: 26 additions & 0 deletions internal/service/apprunner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ func ResourceService() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice(apprunner.Runtime_Values(), false),
},
"runtime_environment_secrets": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 2048),
},
},
"runtime_environment_variables": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -366,6 +374,14 @@ func ResourceService() *schema.Resource {
Default: "8080",
ValidateFunc: validation.StringLenBetween(0, 51200),
},
"runtime_environment_secrets": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 2048),
},
},
"runtime_environment_variables": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -918,6 +934,10 @@ func expandServiceImageConfiguration(l []interface{}) *apprunner.ImageConfigurat
result.Port = aws.String(v)
}

if v, ok := tfMap["runtime_environment_secrets"].(map[string]interface{}); ok && len(v) > 0 {
result.RuntimeEnvironmentSecrets = flex.ExpandStringMap(v)
}

if v, ok := tfMap["runtime_environment_variables"].(map[string]interface{}); ok && len(v) > 0 {
result.RuntimeEnvironmentVariables = flex.ExpandStringMap(v)
}
Expand Down Expand Up @@ -1034,6 +1054,10 @@ func expandServiceCodeConfigurationValues(l []interface{}) *apprunner.CodeConfig
result.Runtime = aws.String(v)
}

if v, ok := tfMap["runtime_environment_secrets"].(map[string]interface{}); ok && len(v) > 0 {
result.RuntimeEnvironmentSecrets = flex.ExpandStringMap(v)
}

if v, ok := tfMap["runtime_environment_variables"].(map[string]interface{}); ok && len(v) > 0 {
result.RuntimeEnvironmentVariables = flex.ExpandStringMap(v)
}
Expand Down Expand Up @@ -1199,6 +1223,7 @@ func flattenServiceCodeConfigurationValues(values *apprunner.CodeConfigurationVa
"build_command": aws.StringValue(values.BuildCommand),
"port": aws.StringValue(values.Port),
"runtime": aws.StringValue(values.Runtime),
"runtime_environment_secrets": aws.StringValueMap(values.RuntimeEnvironmentSecrets),
"runtime_environment_variables": aws.StringValueMap(values.RuntimeEnvironmentVariables),
"start_command": aws.StringValue(values.StartCommand),
}
Expand Down Expand Up @@ -1254,6 +1279,7 @@ func flattenServiceImageConfiguration(config *apprunner.ImageConfiguration) []in

m := map[string]interface{}{
"port": aws.StringValue(config.Port),
"runtime_environment_secrets": aws.StringValueMap(config.RuntimeEnvironmentSecrets),
"runtime_environment_variables": aws.StringValueMap(config.RuntimeEnvironmentVariables),
"start_command": aws.StringValue(config.StartCommand),
}
Expand Down
91 changes: 91 additions & 0 deletions internal/service/apprunner/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccAppRunnerService_ImageRepository_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "service_name", rName),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "apprunner", regexp.MustCompile(fmt.Sprintf(`service/%s/.+`, rName))),
acctest.MatchResourceAttrRegionalARN(resourceName, "auto_scaling_configuration_arn", "apprunner", regexp.MustCompile(`autoscalingconfiguration/DefaultConfiguration/1/.+`)),
resource.TestCheckResourceAttr(resourceName, "encryption_configuration.#", "0"),
resource.TestCheckResourceAttr(resourceName, "health_check_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "health_check_configuration.0.protocol", apprunner.HealthCheckProtocolTcp),
resource.TestCheckResourceAttr(resourceName, "health_check_configuration.0.path", "/"),
Expand All @@ -53,13 +54,18 @@ func TestAccAppRunnerService_ImageRepository_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "network_configuration.0.egress_configuration.0.vpc_connector_arn", ""),
resource.TestCheckResourceAttr(resourceName, "network_configuration.0.ingress_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "network_configuration.0.ingress_configuration.0.is_publicly_accessible", "true"),
resource.TestCheckResourceAttr(resourceName, "observability_configuration.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "service_id"),
resource.TestCheckResourceAttrSet(resourceName, "service_url"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.authentication_configuration.#", "0"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.auto_deployments_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.code_repository.#", "0"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_configuration.0.port", "80"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_configuration.0.runtime_environment_secrets.%", "0"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_configuration.0.runtime_environment_variables.%", "0"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_identifier", "public.ecr.aws/nginx/nginx:latest"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_repository_type", apprunner.ImageRepositoryTypeEcrPublic),
resource.TestCheckResourceAttr(resourceName, "status", apprunner.ServiceStatusRunning),
Expand Down Expand Up @@ -378,6 +384,37 @@ func TestAccAppRunnerService_ImageRepository_runtimeEnvironmentVars(t *testing.T
})
}

func TestAccAppRunnerService_ImageRepository_runtimeEnvironmentSecrets(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_apprunner_service.test"
ssmParameterResourceName := "aws_ssm_parameter.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, apprunner.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceConfig_ImageRepository_runtimeEnvSecrets(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "source_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source_configuration.0.image_repository.0.image_configuration.0.runtime_environment_secrets.%", "1"),
resource.TestCheckResourceAttrPair(resourceName, "source_configuration.0.image_repository.0.image_configuration.0.runtime_environment_secrets.SSM_PARAMETER", ssmParameterResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAppRunnerService_disappears(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_apprunner_service.test"
Expand Down Expand Up @@ -561,6 +598,60 @@ resource "aws_apprunner_service" "test" {
`, rName)
}

func testAccServiceConfig_ImageRepository_runtimeEnvSecrets(rName string) string {
return acctest.ConfigCompose(
testAccIAMRole(rName),
fmt.Sprintf(`
resource "aws_ssm_parameter" "test" {
name = %[1]q
type = "String"
value = "test"
}
resource "aws_iam_role_policy" "test_policy" {
name = %[1]q
role = aws_iam_role.test.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ssm:GetParameters",
]
Effect = "Allow"
Resource = [
aws_ssm_parameter.test.arn
]
},
]
})
}
resource "aws_apprunner_service" "test" {
service_name = %[1]q
source_configuration {
auto_deployments_enabled = false
image_repository {
image_configuration {
port = "80"
runtime_environment_secrets = {
SSM_PARAMETER = aws_ssm_parameter.test.arn
}
}
image_identifier = "public.ecr.aws/nginx/nginx:latest"
image_repository_type = "ECR_PUBLIC"
}
}
instance_configuration {
cpu = "1 vCPU"
instance_role_arn = aws_iam_role.test.arn
memory = "3 GB"
}
}
`, rName))
}

func testAccServiceConfig_ImageRepository_autoScalingConfiguration(rName string) string {
return fmt.Sprintf(`
resource "aws_apprunner_auto_scaling_configuration_version" "test" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/apprunner_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ The `code_configuration_values` blocks supports the following arguments:
* `build_command` - (Optional) Command App Runner runs to build your application.
* `port` - (Optional) Port that your application listens to in the container. Defaults to `"8080"`.
* `runtime` - (Required) Runtime environment type for building and running an App Runner service. Represents a programming language runtime. Valid values: `PYTHON_3`, `NODEJS_12`, `NODEJS_14`, `NODEJS_16`, `CORRETTO_8`, `CORRETTO_11`, `GO_1`, `DOTNET_6`, `PHP_81`, `RUBY_31`.
* `runtime_environment_secrets` - (Optional) Secrets and parameters available to your service as environment variables. A map of key/value pairs.
* `runtime_environment_variables` - (Optional) Environment variables available to your running App Runner service. A map of key/value pairs. Keys with a prefix of `AWSAPPRUNNER` are reserved for system use and aren't valid.
* `start_command` - (Optional) Command App Runner runs to start your application.

Expand All @@ -244,6 +245,7 @@ The `code_configuration_values` blocks supports the following arguments:
The `image_configuration` block supports the following arguments:

* `port` - (Optional) Port that your application listens to in the container. Defaults to `"8080"`.
* `runtime_environment_secrets` - (Optional) Secrets and parameters available to your service as environment variables. A map of key/value pairs.
* `runtime_environment_variables` - (Optional) Environment variables available to your running App Runner service. A map of key/value pairs. Keys with a prefix of `AWSAPPRUNNER` are reserved for system use and aren't valid.
* `start_command` - (Optional) Command App Runner runs to start the application in the source image. If specified, this command overrides the Docker image’s default start command.

Expand Down

0 comments on commit 950252e

Please sign in to comment.