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

fix(apprunner-alpha): env vars and secrets can't solely be added via .add*() methods #24346

Merged
merged 9 commits into from
Mar 3, 2023
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-apprunner/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ export class Service extends cdk.Resource {
}

private renderEnvironmentVariables(): EnvironmentVariable[] | undefined {
if (Object.keys(this.environmentVariables).length > 0) {
if (Object.keys(this.environmentVariables).length + this.variables.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`);
Expand All @@ -1151,7 +1151,7 @@ export class Service extends cdk.Resource {
}

private renderEnvironmentSecrets(): EnvironmentSecret[] | undefined {
if (Object.keys(this.environmentSecrets).length > 0 && this.instanceRole) {
if (Object.keys(this.environmentSecrets).length + this.secrets.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`);
Expand Down
62 changes: 62 additions & 0 deletions packages/@aws-cdk/aws-apprunner/test/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,68 @@ test('custom environment secrets and start commands are allowed for imageConfigu
});
});


test('custom environment variables and secrets can be added without first defining them in props', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'demo-stack');
// WHEN
const secret = new secretsmanager.Secret(stack, 'Secret');
const service = new apprunner.Service(stack, 'DemoService', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: {
startCommand: '/root/start-command.sh',
},
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
});

service.addEnvironmentVariable('TEST_ENVIRONMENT_VARIABLE', 'test environment variable value');
service.addSecret('LATER_SECRET', apprunner.Secret.fromSecretsManager(secret, 'field'));

// THEN
// we should have the service
Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::Service', {
SourceConfiguration: {
AuthenticationConfiguration: {},
ImageRepository: {
ImageConfiguration: {
RuntimeEnvironmentVariables: [
{
Name: 'TEST_ENVIRONMENT_VARIABLE',
Value: 'test environment variable value',
},
],
RuntimeEnvironmentSecrets: [
{
Name: 'LATER_SECRET',
Value: {
'Fn::Join': [
'',
[
{
Ref: 'SecretA720EF05',
},
':field::',
],
],
},
},
],
StartCommand: '/root/start-command.sh',
},
ImageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
ImageRepositoryType: 'ECR_PUBLIC',
},
},
NetworkConfiguration: {
EgressConfiguration: {
EgressType: 'DEFAULT',
},
},
});
});

test('create a service from existing ECR repository(image repository type: ECR)', () => {
// GIVEN
const app = new cdk.App();
Expand Down