Skip to content

Commit

Permalink
Fix region error handling in Lambda@Edge implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuens committed Oct 25, 2019
1 parent 56f3c56 commit 83db633
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/package/compile/events/cloudFront/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require('lodash');
const url = require('url');
const chalk = require('chalk');
const ServerlessError = require('../../../../../../classes/Error');
const { ServerlessError } = require('../../../../../../classes/Error');

class AwsCompileCloudFrontEvents {
constructor(serverless, options) {
Expand Down
87 changes: 83 additions & 4 deletions lib/plugins/aws/package/compile/events/cloudFront/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const sandbox = require('sinon');
const AwsProvider = require('../../../../provider/awsProvider');
const AwsCompileCloudFrontEvents = require('./index');
const Serverless = require('../../../../../../Serverless');
const { ServerlessError } = require('../../../../../../classes/Error');

chai.use(require('sinon-chai'));

Expand All @@ -13,12 +14,13 @@ const { expect } = chai;
describe('AwsCompileCloudFrontEvents', () => {
let serverless;
let awsCompileCloudFrontEvents;
const options = {
stage: 'dev',
region: 'us-east-1',
};
let options;

beforeEach(() => {
options = {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless.processedInput = {
commands: [],
Expand Down Expand Up @@ -265,6 +267,83 @@ describe('AwsCompileCloudFrontEvents', () => {
expect(() => awsCompileCloudFrontEvents.compileCloudFrontEvents()).to.throw(Error);
});

it('should throw an error if the region is not us-east-1', () => {
options.region = 'eu-central-1';
awsCompileCloudFrontEvents.serverless.service.functions = {
first: {
name: 'first',
events: [
{
cloudFront: {
eventType: 'viewer-request',
origin: 's3://bucketname.s3.amazonaws.com/files',
},
},
],
},
};
awsCompileCloudFrontEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources = {
FirstLambdaFunction: {
Type: 'AWS::Lambda::Function',
Properties: {
FunctionName: 'first',
},
},
FirstLambdaVersion: {
Type: 'AWS::Lambda::Version',
Properties: {
FunctionName: { Ref: 'FirstLambdaFunction' },
},
},
IamRoleLambdaExecution: {
Type: 'AWS::IAM::Role',
Properties: {
AssumeRolePolicyDocument: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: {
Service: ['lambda.amazonaws.com'],
},
Action: ['sts:AssumeRole'],
},
],
},
Policies: [
{
PolicyName: {
'Fn::Join': ['-', ['dev', 'first', 'lambda']],
},
PolicyDocument: {
Version: '2012-10-17',
Statement: [],
},
},
],
Path: '/',
RoleName: {
'Fn::Join': [
'-',
[
'first',
'dev',
{
Ref: 'AWS::Region',
},
'lambdaRole',
],
],
},
},
},
};

expect(() => awsCompileCloudFrontEvents.compileCloudFrontEvents()).to.throw(
/to the us-east-1 region/
);
});

it('should create corresponding resources when cloudFront events are given', () => {
awsCompileCloudFrontEvents.serverless.service.functions = {
first: {
Expand Down

0 comments on commit 83db633

Please sign in to comment.