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(amplify-category-function): use ref for S3Bucket and S3Key in CFN #6358

Merged
merged 5 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
"Default" : "NONE",
"Description" : " Schedule Expression"
},
"deploymentBucketName": {
"Type": "String"
},
"env": {
"Type": "String"
},
"s3Key": {
"Type": "String"
}<%if (props.dependsOn && props.dependsOn.length > 0) { %>,<% } %>
<% if (props.dependsOn) { %>
<% for(var i=0; i < props.dependsOn.length; i++) { %>
Expand Down Expand Up @@ -45,6 +51,14 @@
"aws:asset:property": "Code"
},
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "deploymentBucketName"
},
"S3Key": {
"Ref": "s3Key"
}
},
"Handler": "<%= props.functionTemplate.handler? props.functionTemplate.handler : props.runtime.defaultHandler %>",
"FunctionName": {
"Fn::If": [
Expand All @@ -68,7 +82,7 @@
"Environment": {
"Variables" : <%- JSON.stringify(props.environmentMap) %>
},
"Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
"Role": { "Fn::GetAtt": ["LambdaExecutionRole", "Arn"] },
"Runtime": "<%= props.runtime.cloudTemplateValue %>",
"Layers": <%- JSON.stringify(props.lambdaLayersCFNArray) %>,
"Timeout": "25"
Expand Down Expand Up @@ -125,10 +139,10 @@
"Statement": [
{
"Effect": "Allow",
"Action":["logs:CreateLogGroup",
"Action": ["logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"],
"Resource": { "Fn::Sub" : [ "arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*", { "region": {"Ref": "AWS::Region"}, "account": {"Ref": "AWS::AccountId"}, "lambda": {"Ref": "LambdaFunction"}} ]}
"Resource": { "Fn::Sub": [ "arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*", { "region": {"Ref": "AWS::Region"}, "account": {"Ref": "AWS::AccountId"}, "lambda": {"Ref": "LambdaFunction"}} ]}
}<% if (props.functionTemplate.parameters && props.functionTemplate.parameters.database && props.functionTemplate.parameters.database.resourceName) { %>,
{
"Effect": "Allow",
Expand Down Expand Up @@ -214,17 +228,17 @@
<% } %>
<% if (props.cloudwatchRule && props.cloudwatchRule != "NONE") { %>
,"CloudWatchEvent": {
"Type" : "AWS::Events::Rule",
"Properties":{
"Description" : "Schedule rule for Lambda",
"ScheduleExpression" : {
"Ref" : "CloudWatchRule"
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Schedule rule for Lambda",
"ScheduleExpression": {
"Ref": "CloudWatchRule"
},
"State": "ENABLED",
"Targets": [{
"Arn": { "Fn::GetAtt": ["LambdaFunction", "Arn"] },
"Id":{
"Ref" : "LambdaFunction"
"Id": {
"Ref": "LambdaFunction"
}
}]
}
Expand Down Expand Up @@ -263,8 +277,8 @@
}
<% if (props.cloudwatchRule && props.cloudwatchRule != "NONE") { %>
,"CloudWatchEventRule": {
"Value" :{
"Ref" : "CloudWatchEvent"
"Value": {
"Ref": "CloudWatchEvent"
}
}
<% } %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONUtilities } from 'amplify-cli-core';
import { JSONUtilities, pathManager } from 'amplify-cli-core';
import { LambdaLayer } from 'amplify-function-plugin-interface';
import { saveMutableState } from '../../../../provider-utils/awscloudformation/utils/storeResources';

Expand All @@ -7,23 +7,21 @@ jest.mock('amplify-cli-core', () => ({
readJson: jest.fn(),
writeJson: jest.fn(),
},
pathManager: {
getBackendDirPath: jest.fn(),
},
}));

const JSONUtilities_mock = JSONUtilities as jest.Mocked<typeof JSONUtilities>;
const pathManager_mock = pathManager as jest.Mocked<typeof pathManager>;

pathManager_mock.getBackendDirPath.mockImplementation(() => 'backendDir');

describe('save mutable state', () => {
beforeEach(() => {
jest.clearAllMocks();
});

const context_stub = {
amplify: {
pathManager: {
getBackendDirPath: () => 'backendDir',
},
},
};

it('destructures mutableParametersState in the stored object', () => {
const mutableParametersStateContents = {
permissions: {
Expand All @@ -36,7 +34,7 @@ describe('save mutable state', () => {
lambdaLayers: [] as LambdaLayer[],
};

saveMutableState(context_stub, input);
saveMutableState(input);
expect(JSONUtilities_mock.writeJson.mock.calls[0][1]).toMatchSnapshot();
});

Expand All @@ -62,7 +60,7 @@ describe('save mutable state', () => {
lambdaLayers: [],
resourceName: 'testResourceName',
};
saveMutableState(context_stub, input);
saveMutableState(input);
expect(JSONUtilities_mock.writeJson.mock.calls[0][1]).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ export async function updateFunctionResource(context, category, service, paramet
parameters = _.assign(parameters, previousParameters);
}
}
saveMutableState(context, parameters);
saveMutableState(parameters);
} else {
parameters = await serviceConfig.walkthroughs.updateWalkthrough(context, parameters, resourceToUpdate);
if (parameters.dependsOn) {
context.amplify.updateamplifyMetaAfterResourceUpdate(category, parameters.resourceName, 'dependsOn', parameters.dependsOn);
}
saveMutableState(context, parameters);
saveCFNParameters(context, parameters);
saveMutableState(parameters);
saveCFNParameters(parameters);
}

if (!parameters || (parameters && !parameters.skipEdit)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { $TSContext } from 'amplify-cli-core';
import { Fn, DeletionPolicy, Refs } from 'cloudform-types';
import _ from 'lodash';
import Lambda from 'cloudform-types/types/lambda';
import { isMultiEnvLayer, Permission, LayerParameters, getLayerMetadataFactory, LayerMetadata } from './layerParams';
import { getLayerMetadataFactory, isMultiEnvLayer, LayerMetadata, LayerParameters, Permission } from './layerParams';

function generateLayerCfnObjBase(multiEnvLayer: boolean) {
function generateLayerCfnObjBase() {
const cfnObj = {
AWSTemplateFormatVersion: '2010-09-09',
Description: 'Lambda layer resource stack creation using Amplify CLI',
Expand All @@ -15,33 +16,26 @@ function generateLayerCfnObjBase(multiEnvLayer: boolean) {
env: {
Type: 'String',
},
s3Key: {
Type: 'String',
},
deploymentBucketName: {
Type: 'String',
},
},
Resources: {},
Conditions: {
HasEnvironmentParameter: Fn.Not(Fn.Equals(Fn.Ref('env'), 'NONE')),
},
};

if (multiEnvLayer) {
_.merge(cfnObj, {
Parameters: {
s3Key: {
Type: 'String',
},
deploymentBucketName: {
Type: 'String',
},
},
});
}

return cfnObj;
}

/**
* generates CFN for Layer and Layer permissions when updating layerVersion
*/
export function generateLayerCfnObj(context, parameters: LayerParameters) {
export function generateLayerCfnObj(context: $TSContext, parameters: LayerParameters) {
const multiEnvLayer = isMultiEnvLayer(context, parameters.layerName);
const layerData = getLayerMetadataFactory(context)(parameters.layerName);
const outputObj = {
Expand All @@ -52,7 +46,7 @@ export function generateLayerCfnObj(context, parameters: LayerParameters) {
Region: { Value: Refs.Region },
},
};
let cfnObj = { ...generateLayerCfnObjBase(multiEnvLayer), ...outputObj };
let cfnObj = { ...generateLayerCfnObjBase(), ...outputObj };
const POLICY_RETAIN = DeletionPolicy.Retain;
const layerName = multiEnvLayer ? Fn.Sub(`${parameters.layerName}-` + '${env}', { env: Fn.Ref('env') }) : parameters.layerName;

Expand Down