aws-cloudformation / aws-cloudformation-templates

A collection of useful CloudFormation templates
Apache License 2.0
4.69k stars 4.28k forks source link

get_vpc_main_route_table_id #218

Closed vennemp closed 6 months ago

vennemp commented 5 years ago

Implemented this into a CFN (basically trying to create a VPC, and attach it to a TGW and add a route to the TGW in the main route table) and it got hung on the lambdatrigger creation.

I browsed to the lambda console before deleting the stack and it said "Lambda can't find the file Routetable.py. Make sure that your handler upholds the format: file-name.method."

I read there was a trick to creating the zip file but wasn't fruitful in my efforts (if that was indeed the issue).

AWSTemplateFormatVersion: 2010-09-09
Description: Creates a VPC, attaches it to the Transit Gateway, and launches the desired instances
Parameters: 
  VPCCidrBlock:
    AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
    Description: VPC CIDR Block (eg 172.25.3.0/24)
    Type: String
  NumberOfInstances: 
    Default: 1
    Type: Number
    Description: Number of instances
  AMIID:
    Default: ami-09c61c4850b7465cb
    Type: String
    Description: AMI-ID of desired instances
  EmailAddress:
    Type: String 
    Description: Email address for notification
  CAFETransitGateway:
    Type: String
    Description: Transit Gateway ID 
    Default: tgw-xxxxxxxxxxxx
  Bucket:
    Type: String
    Default: cfn-mainroutetableidlookup
    Description: Bucket name where python script is stored
  Key:
    Type: String
    Description: Zip file name
    Default: routetable.zip
  Lambdahandler:
    Type: String
    Description: Name of the python file(lambda handler)
    Default: Routetable

Resources:
  DHCPOptions:
    Type: AWS::EC2::DHCPOptions
    Properties:
      DomainName: domain.com
      DomainNameServers: 
        - 10.0.0.10,10.0.0.11

  ResearchVPC:
    DependsOn: DHCPOptions
    Type: AWS::EC2::VPC
    Properties: 
      CidrBlock: !Ref VPCCidrBlock
      EnableDnsHostnames: true
      EnableDnsSupport: true

  DHCPOptionsAssociation:
    DependsOn: ResearchVPC
    Type: AWS::EC2::VPCDHCPOptionsAssociation
    Properties:
      VpcId: !Ref ResearchVPC
      DhcpOptionsId: !Ref DHCPOptions

  InitialSubnet:
    DependsOn: ResearchVPC
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref ResearchVPC
      CidrBlock: !Select [ 0, !Cidr [ !GetAtt ResearchVPC.CidrBlock, 1, 5 ]]

  TGWAttachment:
    DependsOn: InitialSubnet
    Type: AWS::EC2::TransitGatewayAttachment
    Properties: 
      SubnetIds: 
        - !Ref InitialSubnet
      VpcId: !Ref ResearchVPC
      TransitGatewayId: !Ref CAFETransitGateway

  LambdaIAMRole:
    Type: 'AWS::IAM::Role'
    DependsOn: ResearchVPC
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 'ec2:Describe*'
                Resource: '*'
              - Effect: Allow
                Action:
                  - 'logs:CreateLogGroup'
                  - 'logs:CreateLogStream'
                  - 'logs:PutLogEvents'
                Resource: 'arn:aws:logs:*:*:*'

  LambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: !Sub '${Lambdahandler}.lambda_handler'
      Role: !GetAtt LambdaIAMRole.Arn
      Code:
        S3Bucket: !Ref Bucket
        S3Key: !Ref Key
      Runtime: python3.6
      Timeout: 50

  Lambdatrigger:
    Type: 'Custom::RouteTableLambda'
    Properties:
      ServiceToken: !GetAtt LambdaFunction.Arn
      VPCID: !Ref ResearchVPC

  TGWRouteTableEntry:
    Type: AWS::EC2::Route
    DependsOn: InitialSubnet
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      TransitGatewayId: !Ref CAFETransitGateway
      RouteTableId: !GetAtt Lambdatrigger.RoutetableID
github-actions[bot] commented 6 months ago

Due to inactivity this issue will be closed in 7 days