arc-s3-bucket
Advanced tools
Comparing version 1.0.2 to 2.0.0
86
index.js
@@ -1,3 +0,1 @@ | ||
const caps = require('lodash.capitalize'); | ||
/** | ||
@@ -9,62 +7,32 @@ * @param {object} arc - the parsed .arc file currently executing | ||
module.exports = function arcBucketMacro(arc, cloudformation, stage) { | ||
const config = arc.s3; | ||
const buckets = {}; | ||
// set the S3_BUCKET env variable for every lambda we create | ||
// first get the correct path names with the correct formatting: | ||
const paths = arc.http.map(list => { | ||
return list.map(component => { | ||
const p = component.replace(/\//g, ''); | ||
const pList = p.split(':'); | ||
return pList.map(caps).join('') | ||
}).join(''); | ||
if (!arc.s3) { | ||
return cloudformation; | ||
} | ||
cloudformation.Resources.ArcS3Bucket = { | ||
Type: 'AWS::S3::Bucket' | ||
}; | ||
Object.entries(cloudformation.Resources).forEach(([key, value]) => { | ||
if (value.Type && value.Type === 'AWS::Serverless::Function') { | ||
cloudformation.Resources[key].Properties.Environment.Variables.S3_BUCKET = { Ref: 'ArcS3Bucket' }; | ||
} | ||
}); | ||
let s3Bucket; | ||
arc.aws.forEach(f => { | ||
if (f[0] === 'bucket') { | ||
s3Bucket = f[1]; | ||
cloudformation.Resources.Role.Properties.Policies.push({ | ||
PolicyName: 'ArcS3BucketAccess', | ||
PolicyDocument: { | ||
Statement: [{ | ||
Effect: 'Allow', | ||
Action: 's3:*', | ||
Resource: { 'Fn::GetAtt': ['ArcS3Bucket', 'Arn'] } | ||
}] | ||
} | ||
}); | ||
s3Bucket = process.env.S3_BUCKET || s3Bucket; | ||
// loop over every path and set the ENV var: | ||
paths.map(p => { | ||
if (p === 'Get') { | ||
p = 'GetIndex'; | ||
cloudformation.Outputs.s3Bucket = { | ||
Description: 'S3 Bucket', | ||
Value: { | ||
Ref: 'ArcS3Bucket' | ||
} | ||
if (cloudformation.Resources[p]) { | ||
cloudformation.Resources[p].Properties.Environment.Variables.S3_BUCKET = s3Bucket; | ||
} | ||
}) | ||
// for each bucket: | ||
config.map(async b => { | ||
let bucketName = b[0]; | ||
if (b.length > 1) { | ||
// abort if bucket does not match requested stage: | ||
if (b[0] !== stage) { | ||
return; | ||
} | ||
// bucketname will be "{name}-production" or "{name}-staging" | ||
// if bucket is marked 'unique' it will have a timestamp appended to the end: | ||
bucketName = `${b[1]}${b[2] && b[2].toLowerCase() === 'unique' ? new Date().getTime() : ''}`; | ||
} | ||
cloudformation.Resources[bucketName] = { | ||
Type: "AWS::S3::Bucket", | ||
Properties: { | ||
BucketName: bucketName, | ||
// todo: any other properties we want? | ||
} | ||
}; | ||
const env = process.env.S3_BUCKET || ''; | ||
// modify arcformation | ||
cloudformation.Resources.Role.Properties.Policies.push({ | ||
PolicyName: `ArcS3Access-${bucketName}`, | ||
PolicyDocument: { | ||
Statement: [{ | ||
Effect: 'Allow', | ||
Action: 's3:*', | ||
Resource: `arn:aws:s3:::${bucketName}/*` | ||
}] | ||
} | ||
}); | ||
}); | ||
return cloudformation | ||
} | ||
}; | ||
return cloudformation; | ||
}; |
{ | ||
"name": "arc-s3-bucket", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "arc macro for making s3 buckets", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
1
2961
72