# Hello world API using AWS CloudFormation ```yaml= AWSTemplateFormatVersion: 2010-09-09 Description: | Not using SAM Resources: # First you need to grant API Gateway permession to use Lambda HelloWorldFunctionHelloWorldPermissionProd: Type: 'AWS::Lambda::Permission' Properties: Action: 'lambda:InvokeFunction' FunctionName: !Ref HelloWorldFunction Principal: apigateway.amazonaws.com SourceArn: !Sub - >- arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/hello - __ApiId__: !Ref ServerlessRestApi __Stage__: '*' # IAM Role for Lambda HelloWorldFunctionRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Action: - 'sts:AssumeRole' Effect: Allow Principal: Service: - lambda.amazonaws.com ManagedPolicyArns: - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' Tags: - Key: 'lambda:createdBy' Value: SAM # API Gateway stage ServerlessRestApiProdStage: Type: 'AWS::ApiGateway::Stage' Properties: DeploymentId: !Ref ServerlessRestApiDeployment47fc2d5f9d RestApiId: !Ref ServerlessRestApi StageName: Prod # Deploy the API Gateway ServerlessRestApiDeployment47fc2d5f9d: Type: 'AWS::ApiGateway::Deployment' Properties: Description: 'RestApi deployment id: 47fc2d5f9d21ad56f83937abe2779d0e26d7095e' RestApiId: !Ref ServerlessRestApi StageName: Stage # The API Gateway ServerlessRestApi: Type: 'AWS::ApiGateway::RestApi' Properties: Body: info: version: '1.0' title: !Ref 'AWS::StackName' paths: /hello: get: x-amazon-apigateway-integration: httpMethod: POST type: aws_proxy uri: !Sub >- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations responses: {} swagger: '2.0' # ok now the Function HelloWorldFunction: Type: 'AWS::Lambda::Function' Metadata: SamResourceId: HelloWorldFunction Properties: Code: S3Bucket: aws-sam-cli-managed-default-samclisourcebucket-1bqeiayem2z8g S3Key: 2f79732113d63e8bed26938ff91dc59f Handler: app.lambda_handler Role: !GetAtt - HelloWorldFunctionRole - Arn Runtime: python3.10 Timeout: 3 Tags: - Key: 'lambda:createdBy' Value: SAM Architectures: - x86_64 Outputs: HelloWorldApi: Description: API Gateway endpoint URL for Prod stage for Hello World function Value: !Sub >- https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/ HelloWorldFunction: Description: Hello World Lambda Function ARN Value: !GetAtt - HelloWorldFunction - Arn HelloWorldFunctionIamRole: Description: Implicit IAM Role created for Hello World function Value: !GetAtt - HelloWorldFunctionRole - Arn ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up