0% found this document useful (0 votes)
5 views2 pages

CloudFormation With API Gateway

This CloudFormation YAML template creates an application backend using AWS resources, including a Lambda function, an IAM role, and an API Gateway. It defines the necessary configurations for the Lambda function and sets up an API Gateway with a resource and method to invoke the function. The template also includes an output for the backend API URL and requires customization for specific needs.

Uploaded by

saiakkina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

CloudFormation With API Gateway

This CloudFormation YAML template creates an application backend using AWS resources, including a Lambda function, an IAM role, and an API Gateway. It defines the necessary configurations for the Lambda function and sets up an API Gateway with a resource and method to invoke the function. The template also includes an output for the backend API URL and requires customization for specific needs.

Uploaded by

saiakkina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Certainly, here's a CloudFormation YAML template that includes AWS resources for an

application backend with an API Gateway:

```yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for an application backend with API Gateway

Resources:
# Define the Lambda function for the backend
BackendLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: MyBackendFunction
Handler: index.handler
Role: !GetAtt BackendLambdaExecutionRole.Arn
Code:
S3Bucket: your-s3-bucket
S3Key: your-lambda-code.zip
Runtime: nodejs14.x

# Define the IAM role for the Lambda function


BackendLambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: BackendLambdaPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action: # Add necessary permissions for your backend
- # Add necessary actions here
Resource: '*'

# Define the API Gateway


BackendApiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: MyBackendApi
FailOnWarnings: true

# Define the API Gateway resource


BackendApiResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref BackendApiGateway
ParentId: !GetAtt BackendApiGateway.RootResourceId
PathPart: backend

# Define the API Gateway method


BackendApiMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: POST # Change to the appropriate HTTP method
ResourceId: !Ref BackendApiResource
RestApiId: !Ref BackendApiGateway
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/$
{BackendLambdaFunction.Arn}/invocations

# Define the API Gateway deployment


BackendApiDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref BackendApiGateway

Outputs:
BackendApiUrl:
Description: URL for the backend API
Value: !Sub https://${BackendApiGateway}.execute-api.$
{AWS::Region}.amazonaws.com/prod/backend
```

This template sets up the following resources:

1. A Lambda function for the backend.


2. An IAM role with necessary permissions for the Lambda function.
3. An API Gateway with a resource and method to invoke the Lambda function.
4. An API Gateway deployment.
5. An output that provides the URL for the backend API.

Please remember to customize the template according to your specific needs,


including replacing placeholders such as `your-s3-bucket`, `your-lambda-code.zip`,
and adding the necessary IAM permissions and actions for your backend
functionality.

You might also like