How to add trigger for Lambda, with swagger and RestApi? - amazon-web-services

I'm trying to create Api gateway and lambda function with cloudformation. Current I'm able to create api gateway with swagger, and lambda function. The issue I have is the lambda function dont have the trigger with support to be api gateway. How to add trigger to this lambda function ?
the RestApi Definitation
MyRestApi:
Properties:
ApiKeySourceType: HEADER
BodyS3Location:
Bucket: bucketName
Key: swagger.yaml
Name: OmmaClaimsApi
Type: AWS::ApiGateway::RestApi
The Function Definitation
MyLambda:
Properties:
Code:
S3Bucket: bucketName
S3Key: lambda.zip
FunctionName: MyLambda
Handler: index.handler
MemorySize: 512
Role: !GetAtt 'LambdaExecutionRole.Arn'
Runtime: nodejs8.10
Timeout: 300
Type: AWS::Lambda::Function

Your lambda should go through the API gateway through proxy integration:
/{proxy+}
In your serverless yaml, try to add something like:
functions:
post-transaction:
handler: your.lambda.LambdaHandler
events:
- http:
path: /{proxy+}
method: any

Related

How to set the ResourceId in AWS::ApiGateway::Method if was implicitly created by AWS SAM?

I have an API that is defined as IaC with AWS SAM usign AWS::Serverless::Api for the API Gateway and API Event Source for the Methods.
I developed a method that requires a non lambda proxy integration and some mapping templates in the integration response, so to implement these functionalities in IaC I had to use AWS::ApiGateway::Method from Cloud Formation Templates.
The problem is with the ResourceId property, because is a third level resource in the API, Cloud Formation expects a reference to a AWS::ApiGateway::Resource but the upper levels were implicitly created by AWS SAM with the following code:
PrismShiftDayMacroserviceLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Ref ServiceName
CodeUri: !Ref ServicePath
Role: !Sub 'arn:aws:iam::${AWS::AccountId}:role/lambda_execution_role'
Events:
GetShiftDay:
Type: Api
Properties:
Method: GET
Path: /api/shiftDays/{id}
RequestParameters:
- method.request.path.id
RestApiId: !Ref PrismShiftDayMacroserviceApi
PostShiftDay:
Type: Api
Properties:
RestApiId: !Ref PrismShiftDayMacroserviceApi
Path: /api/shiftDays
Method: POST
SearchShiftDay:
Type: Api
Properties:
RestApiId: !Ref PrismShiftDayMacroserviceApi
Path: /api/shiftDays
Method: GET
PatchShiftDay:
Type: Api
Properties:
RestApiId: !Ref PrismShiftDayMacroserviceApi
Path: /api/shiftDays/{id}
Method: PATCH
Is it possible to get a reference to the resource that was implicitly created by AWS SAM?

How to get reference to aws api gateway in serverless framework

provider:
name: aws
runtime: nodejs14.x
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get
RestApiId:
Ref: TestApi // How to get reference of AWS::Serverless::Api i.e. TestApi here
resources:
Resources:
authFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: handler.hello
TestApi:
DependsOn: AuthFunction
Type: AWS::Serverless::Api
Properties:
StageName: dev
Auth:
DefaultAuthorizer: LambdaRequestAuthorizer
Authorizers:
LambdaRequestAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: !GetAtt AuthFunction.Arn
Getting error:
Configuration error at 'functions.hello': unrecognized property 'RestApiId'
Let's first clarify a few things.
The httpApi event is using HTTP API, not REST API from AWS Api Gateway.
You can set externally created HTTP API by specifying it in the following way:
provider:
httpApi:
id: <your http api reference>
If you'd like to use REST API, then you would need to use http event type and set it like this:
provider:
apiGateway:
restApiId: <your rest api reference>

AWS SAM Unable to call Rekognition and access S3 from Lambda

I am trying to call the detectText method from Rekognition framework and it failed to call S3 bucket. I am not sure how to give roles in SAM Template. Below is my SAM template
GetTextFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: gettextfn/
Handler: text.handler
Runtime: nodejs12.x
Timeout: 3
MemorySize: 128
Environment:
Variables:
imagebucket: !Ref s3bucket
Events:
TextApiEvent:
Type: HttpApi
Properties:
Path: /gettext
Method: get
ApiId: !Ref myapi
Looks like your lambda needs RekognitionDetectOnlyPolicy and also looks you miss the policy to read/write data from S3 bucket also. Have a look at below Policies: added after Environment:
Environment:
Variables:
imagebucket: !Ref s3bucket
Policies:
- S3ReadPolicy:
BucketName: !Ref s3bucket
- RekognitionDetectOnlyPolicy: {}
Events:
You can refer the complete list of AWS SAM policy templates here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html
Also have a look at a sample template here
https://github.com/rollendxavier/serverless_computing/blob/main/template.yaml

Override Authorizer for only one lambda?

I'm taking over a SAM template where the previous dev has an authorizer on API Gateway which forces every lambda function to pass along a Cognito token in order to secure the API.
Auth:
DefaultAuthorizer: AuthStackCognitoAuthorizer
AddDefaultAuthorizerToCorsPreflight: False
Authorizers:
AuthStackCognitoAuthorizer:
UserPoolArn:
Fn::ImportValue:
Fn::Sub: "${AuthStack}-UserPoolArn"
The problem comes in when I want one specific lambda function attached to API Gateway to NOT have an authorizer. Is there a way I can modify my lambda so that it's not requiring the overarching APIGateway Authorizer?
PostFeedbackLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/endpoints/feedback/post
Handler: index.handler
Description: Returns feedback.
Layers:
- !Ref CommonUtilsLayer
Events:
ApiEvent:
Type: Api
Properties:
Method: POST
Path: /feedback
RestApiId: !Ref ApiGatewayApi

SAM Lambda event with an explicit API as the event source

I am trying to set up an event on my lambda function in my SAM template, but I want the event source to be an explicit API endpoint.
The documentation shows an event with an implicit API as an event source:
GetFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.get
Runtime: nodejs6.10
CodeUri: s3://bucket/api_backend.zip
Policies: AmazonDynamoDBReadOnlyAccess
Environment:
Variables:
TABLE_NAME: !Ref Table
Events:
GetResource:
Type: Api
Properties:
Path: /resource/{resourceId}
Method: get
This would be the explicit API definition:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionUri: swagger.yml
How do I explicitly set the event source to be MyApi?
I needed to add the RestApiId under the event definition like so:
Events:
GetResource:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /resource/{resourceId}
Method: get