zoukankan      html  css  js  c++  java
  • [AWS] Lab: CloudFormation and SAM to deploy a Serverless fuction

    Task Breakdown

    1. Install the AWS SAM CLI on your local machine
    2. Create an S3 bucket
    3. Use `sam package` to package our code and save it in S3
    4. Use `sam deploy` to deploy a Lambda function using CloudFormation

    Install SAM CLI

    Guide

    Create an S3 bucket

    Make user your IAM user has access to S3

    aws iam get-user

    Then find the user in IAM to make sure it has 

    `AmazonS3FullAccess`

    Create an S3 bucket

    aws s3 mb s3://<bucket-name> --region us-esat-1

    Resource files:

    Lambda.yml

    AWSTemplateFormatVersion: "2010-09-09"
    Transform: AWS::Serverless-2016-10-31
    Resources:
      TestFunction:
        Type: AWS::Serverless::Function
        Properties:
          Handler: index.handler
          Runtime: nodejs12.x
          Environment:
            Variables:
              S3_BUCKET: <YOUR_BUCKET_NAME>

    index.js

    exports.handler = (event, context, callback) => {
        // TODO implement
        const response = {
            statusCode: 200,
            body: JSON.stringify('Hello Cloud Gurus, This Lambda Function was deployed using SAM!')
        };
        callback(null, response);
    };

    SAM package

    Make sure put those two files in the same folder and run cmd:

    sam package --template-file ./Lambda.yml --output-template-file sam-template.yml --s3-bucket <YOUR_BUCKET_NAME> 

    Replacae <Your_BUCKET_NAME>.

    That cmd will package our code and output `sam-template.yml` file in our folder.

    SAM deploy

    Now it's time to deploy:

    sam deploy --template-file sam-template.yml --stack-name <STACK_NAME> --capabilities CAPABILITY_IAM

    Make sure you have permission to deploy the CloudFormation. If you have AdminAccess then it is fine. Otherwise, you need to give `EC2, CloudFormation, S3, Lambda, IAM full access`.

     

    Lambda

    Last, you can go to Lambda, find newly create lambda, run the test, to make sure, SAM has deploy a Lmabda by using CloudFormation.

    Summary

    If you want to deploy a serverless function, you can use SAM CLI.

    1. It create bucket on S3 and save the CloudFormation template
    2. sam package & sam deploy cmds.
  • 相关阅读:
    GJM :动作手游实时PVP 帧同步(客户端)[转载]
    GJM :多人在线游戏的设计思路
    GJM : 中断被Socket.Accept阻塞的线程
    GJM :异步Socket [转载]
    GJM :C#开发 异步处理是目的,多线程是手段
    GJM : FlatBuffers 与 protobuf 性能比较 [转载 ]
    GJM : Protobuf -NET 相比Json 传输速度只需要1/3 解析只需要1/10
    GJM : Unity3D结合ZXING制作二维码识别
    GJM : Unity3D 常用网络框架与实战解析 【笔记】
    javascripct数组
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14638539.html
Copyright © 2011-2022 走看看