Skip to content

(apprunner-alpha): only using .addEnvrionmentVariable() and .addSecret() fails to work #24345

Closed
@rogerchi

Description

@rogerchi

Describe the bug

Using the apprunner-alpha module, methods .addEnvironmentVariable() and .addSecret() only work if there are existing entries that have been created in the initial imageConfiguration props. This is because in the private renderEnvironmentVariables() and renderEnvironmentSecrets() methods, there is an if condition which returns undefined if there are no entries in the initial imageConfiguration prop. See code here

private renderEnvironmentVariables(): EnvironmentVariable[] | undefined {
if (Object.keys(this.environmentVariables).length > 0) {
for (const [key, value] of Object.entries(this.environmentVariables)) {
if (key.startsWith('AWSAPPRUNNER')) {
throw new Error(`Environment variable key ${key} with a prefix of AWSAPPRUNNER is not allowed`);
}
this.variables.push({ name: key, value: value });
}
return this.variables;
} else {
return undefined;
}
}
private renderEnvironmentSecrets(): EnvironmentSecret[] | undefined {
if (Object.keys(this.environmentSecrets).length > 0 && this.instanceRole) {
for (const [key, value] of Object.entries(this.environmentSecrets)) {
if (key.startsWith('AWSAPPRUNNER')) {
throw new Error(`Environment secret key ${key} with a prefix of AWSAPPRUNNER is not allowed`);
}
value.grantRead(this.instanceRole);
this.secrets.push({ name: key, value: value.arn });
}
return this.secrets;
} else {
return undefined;
}
}

Expected Behavior

Ability to use the methods without having defined initial env variables in the props.

Current Behavior

No environment or secret variables are rendered if only using the .addEnvironmentVariable() and .addSecret() methods.

Reproduction Steps

    const service = new Service(this, 'service', {
      source: Source.fromAsset({
        imageConfiguration: {
          port: 3000,
        },
        asset: imageAsset,
      }),
      instanceRole,
    })
    
    service.addEnvironmentVariable('TEST', 'test')
    service.addSecret('SECRET', Secret.fromSecretsManager(secret)

This will not render any secrets or environment variables in the resulting template, but adding some initial values will cause add environment variables to be correctly rendered, e.g.

    const service = new Service(this, 'service', {
      source: Source.fromAsset({
        imageConfiguration: {
          port: 3000,
          environmentSecrets: { SEED1: Secret.fromSecretsManager(seedSecret) },
          environmentVariables: { SEED2: 'seed' },
        },
        asset: imageAsset,
      }),
      instanceRole,
    })
    
    service.addEnvironmentVariable('TEST', 'test')
    service.addSecret('SECRET', Secret.fromSecretsManager(secret)

Possible Solution

Correct the faulty if logic in renderEnvironmentVariables() and renderEnvironmentSecrets()

Additional Information/Context

No response

CDK CLI Version

2.66.0

Framework Version

No response

Node.js Version

16.17.0

OS

M1 MacOS

Language

Typescript

Language Version

No response

Other information

No response

Activity

added
bugThis issue is a bug.
needs-triageThis issue or PR still needs to be triaged.
on Feb 27, 2023
pahud

pahud commented on Feb 28, 2023

@pahud
Contributor

Thanks for addressing this issue and the PR contribution.

added
effort/mediumMedium work item – several days of effort
and removed
needs-triageThis issue or PR still needs to be triaged.
on Feb 28, 2023
added a commit that references this issue on Mar 3, 2023
45195b6
github-actions

github-actions commented on Mar 3, 2023

@github-actions
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

added a commit that references this issue on Mar 28, 2023
a14a823
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-apprunnerRelated to the apprunner packagebugThis issue is a bug.effort/mediumMedium work item – several days of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @pahud@rogerchi

      Issue actions

        (apprunner-alpha): only using .addEnvrionmentVariable() and .addSecret() fails to work · Issue #24345 · aws/aws-cdk