Skip to content

Instantly share code, notes, and snippets.

@divmgl
Created February 22, 2022 14:12
Show Gist options
  • Select an option

  • Save divmgl/d24e96955cfa50c8aee0170777d847b8 to your computer and use it in GitHub Desktop.

Select an option

Save divmgl/d24e96955cfa50c8aee0170777d847b8 to your computer and use it in GitHub Desktop.

Revisions

  1. divmgl created this gist Feb 22, 2022.
    42 changes: 42 additions & 0 deletions vpc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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
    }
    }
    }