Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineFunction } from '@aws-amplify/backend';
import { defineFunction, secret } from '@aws-amplify/backend';
import type { Backend } from '../../backend';

const branchName = process.env.AWS_BRANCH ?? 'sandbox';
Expand All @@ -12,8 +12,7 @@ export const lowstockproducts = defineFunction({
ENV: `${branchName}`,
REGION: 'us-east-1',
LOW_STOCK_THRESHOLD: '5',
PRODUCT_CATALOG_SECRET:
'/amplify/productcatalog/x/AMPLIFY_lowstockproducts_PRODUCT_CATALOG_SECRET',
PRODUCT_CATALOG_SECRET: secret('PRODUCT_CATALOG_SECRET'),
},
runtime: 22,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,66 @@ describe('FunctionGenerator', () => {
`);
});

it('renders SSM secret env vars as secret() calls', async () => {
const gen1App = await createGen1App({
providers: { awscloudformation: { StackName: 'amplify-test-main-123456', Region: 'us-east-1' } },
function: {
myFunc: {
service: 'Lambda',
output: { Name: 'myFunc-main-abc', Arn: 'arn:aws:lambda:us-east-1:123:function:myFunc-main-abc' },
},
},
});
jest.spyOn(gen1App, 'resourceMetaOutput').mockReturnValue('myFunc-main-abc');
jest.spyOn(gen1App, 'json').mockReturnValue({ Resources: {} });
jest.spyOn(gen1App, 'file').mockReturnValue('{}');
jest.spyOn(gen1App, 'fileExists').mockReturnValue(false);
jest.spyOn(gen1App.aws, 'fetchFunctionConfig').mockResolvedValue({
FunctionName: 'myFunc-main-abc',
Handler: 'index.handler',
Timeout: 3,
MemorySize: 128,
Runtime: 'nodejs18.x',
Environment: {
Variables: {
MY_SECRET: '/amplify/test-app-id/main/AMPLIFY_myFunc_MY_SECRET',
ANOTHER_SECRET: '/amplify/test-app-id/main/AMPLIFY_myFunc_ANOTHER_SECRET',
DB_HOST: 'localhost',
},
},
});
jest.spyOn(gen1App.aws, 'fetchFunctionSchedule').mockResolvedValue(undefined);

const generator = createFunctionGenerator({ gen1App, backendGenerator, packageJsonGenerator, outputDir });
const ops = await generator.plan();
await ops[0].execute();

expect(writtenFile('resource.ts')).toMatchInlineSnapshot(`
"import { defineFunction, secret } from '@aws-amplify/backend';
import type { Backend } from '../../backend';

const branchName = process.env.AWS_BRANCH ?? 'sandbox';

export const myFunc = defineFunction({
entry: './index.js',
name: \`myFunc-\${branchName}\`,
timeoutSeconds: 3,
memoryMB: 128,
environment: {
MY_SECRET: secret('MY_SECRET'),
ANOTHER_SECRET: secret('ANOTHER_SECRET'),
DB_HOST: 'localhost',
},
runtime: 18,
});

export function applyEscapeHatches(backend: Backend) {
backend.myFunc.resources.cfnResources.cfnFunction.functionName = \`myFunc-\${branchName}\`;
}
"
`);
});

it('renders environment variables', async () => {
const gen1App = await createGen1App({
providers: { awscloudformation: { StackName: 'amplify-test-main-123456', Region: 'us-east-1' } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,14 @@ export class FunctionRenderer {
): void {
if (!opts.literalEnvVars || Object.keys(opts.literalEnvVars).length === 0) return;

const ssmSecretPrefix = `/amplify/${this.appId}/${this.backendEnvironmentName}/`;

const envProps = Object.entries(opts.literalEnvVars).map(([key, value]) => {
if (key === 'API_KEY' && value.startsWith(`/amplify/${this.appId}/${this.backendEnvironmentName}`)) {
if (value.startsWith(ssmSecretPrefix)) {
namedImports['@aws-amplify/backend'].add('secret');
return factory.createPropertyAssignment(
key,
factory.createCallExpression(factory.createIdentifier('secret'), undefined, [factory.createStringLiteral('API_KEY')]),
factory.createCallExpression(factory.createIdentifier('secret'), undefined, [factory.createStringLiteral(key)]),
);
}

Expand Down
Loading