Skip to content

Commit 4fb61c2

Browse files
authored
Support by default without policy and role JSON files
1 parent 9701d53 commit 4fb61c2

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

cli.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,27 @@ const deployFunction = function (options) {
321321
if (fs.existsSync(envFilePath)) {
322322
require('dotenv').config({ path: envFilePath })
323323
}
324+
var policyDocument = undefined
325+
var assumeRoleDocument = {
326+
"Version": "2012-10-17", "Statement": [{
327+
"Effect": "Allow", "Principal": {
328+
"Service": "lambda.amazonaws.com"
329+
},
330+
"Action": ["sts:AssumeRole"]
331+
}]
332+
}
324333
const envOptions = { FUNCTION_NAME: functionName, DEPLOYMENT_ENV: envName, DEPLOYMENT_REGION: regionName }
325-
var config = simplify.getInputConfig(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), envOptions)
334+
const configFilePath = path.resolve(configFolderOrFunctionName, configFile || 'config.json')
335+
var config = simplify.getInputConfig(configFilePath, envOptions)
326336
const stackConfigFile = path.resolve(config.OutputFolder, envName || process.env.DEPLOYMENT_ENV, 'StackConfig.json')
327-
var policyDocument = simplify.getContentFile(path.resolve(configFolderOrFunctionName, policyFile || 'policy.json'), envOptions)
328-
var assumeRoleDocument = simplify.getContentFile(path.resolve(configFolderOrFunctionName, roleFile || 'role.json'), envOptions)
337+
const policyFilePath = path.resolve(configFolderOrFunctionName, policyFile || 'policy.json')
338+
if (fs.existsSync(policyFilePath)) {
339+
policyDocument = simplify.getContentFile(policyFilePath, envOptions)
340+
}
341+
const roleFilePath = path.resolve(configFolderOrFunctionName, roleFile || 'role.json')
342+
if (fs.existsSync(roleFilePath)) {
343+
assumeRoleDocument = simplify.getContentFile(roleFilePath, envOptions)
344+
}
329345
if (!fs.existsSync(path.resolve(config.OutputFolder))) {
330346
fs.mkdirSync(path.resolve(config.OutputFolder), { recursive: true })
331347
}
@@ -335,6 +351,14 @@ const deployFunction = function (options) {
335351
functionMeta.lashHash256 = fs.readFileSync(hashFunctionFilePath).toString()
336352
}
337353
return new Promise(function (resolve) {
354+
if (!config.Function) {
355+
config.Function = {
356+
FunctionName: `${functionName}`,
357+
Runtime: process.env.FUNCTION_RUNTIME || 'nodejs14.x',
358+
Handler: process.env.FUNCTION_HANDLER || 'index.handler'
359+
}
360+
simplify.consoleWithMessage(`${opName}-FunctionConfig`, `FunctionName= ${config.Function.FunctionName}, Runtime= ${config.Function.Runtime}, Handler= ${config.Function.Handler}`)
361+
}
338362
provider.setConfig(config).then(_ => {
339363
const roleName = `${config.Function.FunctionName}Role`
340364
return simplify.createOrUpdateFunctionRole({
@@ -458,6 +482,14 @@ const destroyFunction = function (options) {
458482
const hashFunctionFilePath = path.resolve(config.OutputFolder, `${envName || process.env.DEPLOYMENT_ENV}`, `${functionName || process.env.FUNCTION_NAME}.hash`)
459483
const stackList = fs.existsSync(stackConfigFile) ? JSON.parse(fs.readFileSync(stackConfigFile)) : {}
460484
return new Promise(function (resolve) {
485+
if (!config.Function) {
486+
config.Function = {
487+
FunctionName: `${functionName}`,
488+
Runtime: process.env.FUNCTION_RUNTIME || 'nodejs14.x',
489+
Handler: process.env.FUNCTION_HANDLER || 'index.handler'
490+
}
491+
simplify.consoleWithMessage(`${opName}-FunctionConfig`, `FunctionName= ${config.Function.FunctionName}, Runtime= ${config.Function.Runtime}, Handler= ${config.Function.Handler}`)
492+
}
461493
provider.setConfig(config).then(_ => {
462494
const roleName = `${config.Function.FunctionName}Role`
463495
return simplify.deleteFunctionRole({
@@ -473,8 +505,10 @@ const destroyFunction = function (options) {
473505
delete stackList[functionName || process.env.FUNCTION_NAME]
474506
fs.writeFileSync(stackConfigFile, JSON.stringify(stackList, null, 4))
475507
let configInput = JSON.parse(fs.readFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json')))
476-
configInput.Function.Layers = []
477-
fs.writeFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), JSON.stringify(configInput, null, 4))
508+
if (configInput.Function) {
509+
configInput.Function.Layers = []
510+
fs.writeFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), JSON.stringify(configInput, null, 4))
511+
}
478512
fs.unlinkSync(hashFunctionFilePath)
479513
fs.unlinkSync(outputFunctionFilePath)
480514
simplify.consoleWithMessage(`${opName}-DestroyFunction`, `Done. ${data.FunctionName}`)

0 commit comments

Comments
 (0)