Search code examples
aws-lambdaaws-api-gatewayamazon-rdsaws-vpcaws-sam-cli

How to access private AWS resources in AWS SAM LOCAL when start-api testing


I've been working with AWS SAM Local to create and test a lambda / api gateway stack before shipping it to production. I have recently ran into a brick wall when trying to access private resources (RDS) when testing locally (sam local start-api --profile [profile]). I'm able to connect to some of these private resources if I do some ssh tunneling, but was wondering if I am able to test locally without tunneling using VPC.

Below is an example sam template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example Stack

Globals:
    Function:
        Timeout: 3

Resources:
    ExampleFunction:
        Type: 'AWS::Serverless::Function'
        Properties:
            Handler: index.example
            Runtime: nodejs8.10
            CodeUri: .
            Description: 'Just an example'
            MemorySize: 128
            Role: 'arn:aws:iam::[arn-role]'
            VpcConfig:
                SecurityGroupIds:
                    - sg-[12345]
                SubnetIds:
                    - subnet-[12345]
                    - subnet-[23456]
                    - subnet-[34567]
            Events:
                Api1:
                    Type: Api
                    Properties:
                        Path: /example
                        Method: GET

Solution

  • After reading through a lot of documentation and searching stackoverflow for anything that would help... I ended up joining the #samdev slack channel and asked for help. I was provided some guidance and a great guide on setting up OpenVPN on an EC2 instance.

    The set up was super easy (completed in under 30 minutes) and the EC2 instance uses a pre-baked AMI image. Make sure you assign the new EC2 instance to the appropriate VPC containing the resources you need access to.


    Here is a link to the OpenVPN guide: https://openvpn.net/index.php/access-server/on-amazon-cloud.html

    You can request an invite to the #samdev slack channel here: https://awssamopensource.splashthat.com/