Fix handling of arns in GovCloud east and west regions

This commit is contained in:
jmb12686 2019-11-21 16:15:02 -05:00
parent 8e022ba3fe
commit c61dc47f9d
2 changed files with 69 additions and 4 deletions

View File

@ -56,13 +56,24 @@ function response(event, context, status, data = {}, err) {
}
function getLambdaArn(region, accountId, functionName) {
return `arn:aws:lambda:${region}:${accountId}:function:${functionName}`;
let awsNode = 'aws';
if(region === 'us-gov-west-1' || region === 'us-gov-east-2') {
awsNode = 'aws-us-gov';
}
return `arn:${awsNode}:lambda:${region}:${accountId}:function:${functionName}`;
}
function getEnvironment(context) {
const arn = context.invokedFunctionArn.match(
/^arn:aws.*:lambda:(\w+-\w+-\d+):(\d+):function:(.*)$/
);
let arn;
if (context.invokedFunctionArn.includes("arn:aws-us-gov")) {
arn = context.invokedFunctionArn.match(
/^arn:aws-us-gov.*:lambda:(\w+-\w+-\w+-\d+):(\d+):function:(.*)$/
);
} else {
arn = context.invokedFunctionArn.match(
/^arn:aws.*:lambda:(\w+-\w+-\d+):(\d+):function:(.*)$/
);
}
return {
LambdaArn: arn[0],
Region: arn[1],

View File

@ -15,6 +15,28 @@ describe('#getLambdaArn()', () => {
});
});
describe('#getLambdaArn() govloud west', () => {
it('should return the govcloud Lambda arn', () => {
const region = 'us-gov-west-1';
const accountId = '123456';
const functionName = 'some-function';
const arn = getLambdaArn(region, accountId, functionName);
expect(arn).to.equal('arn:aws-us-gov:lambda:us-gov-west-1:123456:function:some-function');
});
});
describe('#getLambdaArn() govcloud east', () => {
it('should return the govcloud Lambda arn', () => {
const region = 'us-gov-east-1';
const accountId = '123456';
const functionName = 'some-function';
const arn = getLambdaArn(region, accountId, functionName);
expect(arn).to.equal('arn:aws-us-gov:lambda:us-gov-east-1:123456:function:some-function');
});
});
describe('#getEnvironment()', () => {
it('should return an object with information about the execution environment', () => {
const context = {
@ -30,3 +52,35 @@ describe('#getEnvironment()', () => {
});
});
});
describe('#getEnvironment() govcloud east', () => {
it('should return an object with information about the govcloud execution environment', () => {
const context = {
invokedFunctionArn: 'arn:aws-us-gov:lambda:us-gov-east-1:123456:function:some-function',
};
const env = getEnvironment(context);
expect(env).to.deep.equal({
LambdaArn: 'arn:aws-us-gov:lambda:us-gov-east-1:123456:function:some-function',
Region: 'us-gov-east-1',
AccountId: '123456',
LambdaName: 'some-function',
});
});
});
describe('#getEnvironment() govcloud west', () => {
it('should return an object with information about the govcloud execution environment', () => {
const context = {
invokedFunctionArn: 'arn:aws-us-gov:lambda:us-gov-west-1:123456:function:some-function',
};
const env = getEnvironment(context);
expect(env).to.deep.equal({
LambdaArn: 'arn:aws-us-gov:lambda:us-gov-west-1:123456:function:some-function',
Region: 'us-gov-west-1',
AccountId: '123456',
LambdaName: 'some-function',
});
});
});