Skip to content

Commit

Permalink
Add MemorySize setting
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinverdonk committed Dec 21, 2019
1 parent 9f1f60b commit 5d1401a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 54 deletions.
1 change: 1 addition & 0 deletions src/lambda.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Resources:
Handler: 'index.@{HandlerName}'
Role: !GetAtt LambdaExecRole.Arn
Runtime: @{Runtime}
MemorySize: @{MemorySize}
ReservedConcurrentExecutions: @{ReservedConcurrentExecutions}
Timeout: @{Timeout}
TracingConfig: {
Expand Down
135 changes: 81 additions & 54 deletions src/lib/generate-cloudformation-template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as fs from 'fs';
import { UploadDeployOptions } from './../';

export const generateCloudFormationTemplate = (options: UploadDeployOptions, lambdaExists: boolean) => {
export const generateCloudFormationTemplate = (
options: UploadDeployOptions,
lambdaExists: boolean
) => {
const template = fs.readFileSync(
`${__dirname}/../../src/lambda${
lambdaExists ? '.version' : ''
Expand All @@ -15,63 +18,87 @@ export const generateCloudFormationTemplate = (options: UploadDeployOptions, lam
.replace(/@{FunctionName}/g, options.functionName)
.replace(/@{HandlerName}/g, options.handlerName)
.replace(/@{Runtime}/g, options.settings.runtime)
.replace(/@{ReservedConcurrentExecutions}/g, (
!Number.isNaN(Number.parseInt(options.settings.reservedConcurrentExecutions as any))
? options.settings.reservedConcurrentExecutions
: '!Ref AWS::NoValue'
).toString())
.replace(/@{MemorySize}/g, options.settings.memory)
.replace(
/@{ReservedConcurrentExecutions}/g,
(!Number.isNaN(
Number.parseInt(options.settings.reservedConcurrentExecutions as any)
)
? options.settings.reservedConcurrentExecutions
: '!Ref AWS::NoValue'
).toString()
)
.replace(/@{Timeout}/g, options.settings.timeout.toString())
.replace(/@{VpcConfig}/g, options.settings.vpcConfig
? JSON.stringify(
Object.keys(options.settings.vpcConfig)
.reduce((acc, key) => {
acc[key.replace(/^\w/, _ => _.toUpperCase())] = (
(options.settings.vpcConfig as any)[key]
)
return acc;
}, {} as any)
)
: '!Ref AWS::NoValue'
.replace(
/@{VpcConfig}/g,
options.settings.vpcConfig
? JSON.stringify(
Object.keys(options.settings.vpcConfig).reduce(
(acc, key) => {
acc[key.replace(/^\w/, _ => _.toUpperCase())] = (options
.settings.vpcConfig as any)[key];
return acc;
},
{} as any
)
)
: '!Ref AWS::NoValue'
)
.replace(/@{Environment}/g, JSON.stringify(options.settings.environment))
.replace(/@{Version}/g, options.version)
.replace(/@{TracingMode}/g, options.settings.tracingConfig.mode)
.replace(/@{RoleStatement}/g, JSON.stringify([{
Effect: 'Allow',
Principal: {
Service: options.settings.servicesAllowed
},
Action: ['sts:AssumeRole']
}]))
.replace(/@{ManagedPolicies}/g, options.settings.managedPolicies
? `ManagedPolicyArns: ${JSON.stringify(
options.settings.managedPolicies
)}`
: ''
.replace(
/@{RoleStatement}/g,
JSON.stringify([
{
Effect: 'Allow',
Principal: {
Service: options.settings.servicesAllowed
},
Action: ['sts:AssumeRole']
}
])
)
.replace(
/@{ManagedPolicies}/g,
options.settings.managedPolicies
? `ManagedPolicyArns: ${JSON.stringify(
options.settings.managedPolicies
)}`
: ''
)
.replace(/@{PolicyStatement}/g, JSON.stringify(
options.settings.permissions.concat([{
effect: 'Allow',
action: [
'lambda:InvokeFunction'
],
resource: [
`arn:aws:lambda:*:*:function:${options.functionName}`,
`arn:aws:lambda:*:*:function:${options.functionName}:*`
]
},{
effect: 'Allow',
action: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents'
],
resource: ['*']
}]).map((_:any) => Object.keys(_)
.reduce((acc, key) => {
acc[key.replace(/^\w/, _ => _.toUpperCase())] = _[key]
return acc;
}, {} as any)
.replace(
/@{PolicyStatement}/g,
JSON.stringify(
options.settings.permissions
.concat([
{
effect: 'Allow',
action: ['lambda:InvokeFunction'],
resource: [
`arn:aws:lambda:*:*:function:${options.functionName}`,
`arn:aws:lambda:*:*:function:${options.functionName}:*`
]
},
{
effect: 'Allow',
action: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents'
],
resource: ['*']
}
])
.map((_: any) =>
Object.keys(_).reduce(
(acc, key) => {
acc[key.replace(/^\w/, _ => _.toUpperCase())] = _[key];
return acc;
},
{} as any
)
)
)
))
};
);
};

0 comments on commit 5d1401a

Please sign in to comment.