Skip to content

Instantly share code, notes, and snippets.

@divmgl
Created February 22, 2022 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divmgl/d24e96955cfa50c8aee0170777d847b8 to your computer and use it in GitHub Desktop.
Save divmgl/d24e96955cfa50c8aee0170777d847b8 to your computer and use it in GitHub Desktop.
Smitten VPC Arc Plugin
const aws = require("aws-sdk")
const iam = new aws.IAM()
const ec2 = new aws.EC2()
const securityGroupNames = [
"allow-internal-ingress",
"allow-internal-egress",
"allow-external-egress"
]
module.exports = {
deploy: {
start: async function ({ cloudformation }) {
const { Resources } = cloudformation
const { AnyCatchallHTTPLambda } = Resources
const { Properties } = AnyCatchallHTTPLambda
const { Subnets } = await ec2.describeSubnets().promise()
const internalSubnet = Subnets.find(vpc =>
vpc.Tags.some(
({ Key, Value }) => Key === "Name" && Value === "internal"
)
)
const { SecurityGroups } = await ec2.describeSecurityGroups().promise()
const securityGroups = SecurityGroups.filter(({ GroupName }) => {
return securityGroupNames.indexOf(GroupName) !== -1
})
const { Roles } = await iam.listRoles().promise()
const { Arn } = Roles.find(({ RoleName }) => RoleName === "lambda-role")
Properties.Role = Arn
Properties.VpcConfig = {
SubnetIds: [internalSubnet.SubnetId],
SecurityGroupIds: securityGroups.map(({ GroupId }) => GroupId)
}
return cloudformation
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment