1
votes

Is it possible to spin up infrastructure such as a SQS queue in front of a Lambda function using AWS SAM without a API gateway?

I only see options to sam local invoke "Lambda" -e event.json and sam local start-api

When I run my lambda which is trying to read messages from the message queue it is not finding the Message Queue URL as referenced below:

NotificationFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
        Handler: index.handler
        Runtime: nodejs8.10
        Role: !Sub ${ConsumerLambdaRole.Arn}
        Timeout: 10

        Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
            Variables:
                NODE_ENV: 'dev'
                MANDRILL_API_KEY: 'PUyIU3DAxJzIlt7KbBE5ow'
                SQS_URL: !Ref MessageQueue


MessageQueue: 
    Type: AWS::SQS::Queue
    Properties: 
        VisibilityTimeout: 60
        RedrivePolicy: 
        deadLetterTargetArn: !Sub ${DeadLetterQueue.Arn}
        maxReceiveCount: 10

# this is where any failed messages will go to
DeadLetterQueue: 
    Type: AWS::SQS::Queue
1
sam local only spins up the lambda part of the sam specification. The rest has to be connected manually by spinning up dockers, checking AWS_SAM_LOCAL to change endpoints in the code etc.fiddur

1 Answers

0
votes

As the comment suggests, sam local invoke will only invoke the code referenced by your lambda resource in the template - it will not create any resources per-se.

What you can do though, is launch your template and either invoke the lambda function using the CLI or via sam local invoke (though you will need to perform an STS role assumption to acquire the correct role for the locally invoked function). However, I assume what you are attempting to do is local testing. In that case, you could do the following:

  1. Download localstack [1]
  2. Use local stack to run a local instance of the SQS service
  3. Configure your function to use the local stack endpoint (of which there is are some good examples available [2][3])

Personally I prefer to avoid local testing, and will tend to launch the template and locally invoke via assumed credentials for debugging purposes. I tend to have an integration test suite that I will execute against sandboxed AWS environment for testing purposes. That said, it takes a fair amount of effort to set that up.

[1] https://github.com/localstack/localstack

[2] https://github.com/billthelizard/aws-sam-localstack-example

[3] http://www.piotrnowicki.com/python/aws/2018/11/16/aws-local-lambda-invocation/