버전 비교

  • 이 줄이 추가되었습니다.
  • 이 줄이 삭제되었습니다.
  • 서식이 변경되었습니다.

...

코드 블럭
languageyml
themeRDark
titleFinal SAM
linenumberstrue
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
  Building Serverless development environment and CI/CD process for DevOps based
  on Cloud9
Globals:
  Function:
    Runtime: python2.7
    Handler: lambda_function.lambda_handler
    MemorySize: 128
    Timeout: 60
    Environment:
      Variables:
        DB_TABLE_NAME:
          Ref: NewsTable
        SNS_TOPIC:
          Ref: NewsTopic
        BUCKET_NAME:
          Ref: PollyMp3Bucket
  Api:
    # enable CORS; to make more specific, change the origin wildcard
    # to a particular domain name, e.g. "'www.example.com'"
    Cors:
      AllowMethods: "'*'"
      AllowHeaders: "'*'"
      AllowOrigin: "'*'"
Resources:
  NewsTable:
    Type: 'AWS::Serverless::SimpleTable'
    Properties:
      PrimaryKey:
        Name: id
        Type: String
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5
  NewsTopic:
    Type: 'AWS::SNS::Topic'
    Properties:
      DisplayName: NewsTopic
  PollyMp3Bucket:
    Type: 'AWS::S3::Bucket'
  StaticWebBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      AccessControl: PublicRead
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html
  PostNews:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: PostNews
      Description: Post news text to convert from text to speech
      Events:
        PostNewsApi:
          Type: Api
          Properties:
            Path: /news
            Method: POST
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:PutItem'
                - 'sns:Publish'
              Resource: '*'
  ConvertAudio:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: ConvertAudio
      Description: Convert Audio using Amazon Polly
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:Query'
                - 'dynamodb:UpdateItem'
                - 's3:GetBucketLocation'
                - 's3:PutObject'
                - 's3:PutObjectAcl'
                - 'polly:SynthesizeSpeech'
              Resource: '*'
      Events:
        ConvertResource:
          Type: SNS
          Properties:
            Topic:
              Ref: NewsTopic
  GetNews:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: GetNews
      Description: Gather information from Ajax calls from web pages
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:Query'
                - 'dynamodb:Scan'
              Resource: '*'
      Events:
        GetNewsApi:
          Type: Api
          Properties:
            Path: /news
            Method: GET
  DeleteNews:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: DeleteNews
      Description: Delete news item in DynamoDB Table and mp3 file in S3 bucket.
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:DeleteItem'
                - 's3:DeleteObject'
              Resource: '*'
      Events:
        DeleteNewsApi:
          Type: Api
          Properties:
            Path: /news
            Method: DELETE
Outputs:
  WebsiteURL:
    Description: Name of S3 bucket to hold website content
    Value:
      'Fn::Join':
        - ''
        - - 'https://'
          - 'Fn::GetAtt':
              - StaticWebBucket
              - DomainName
  APIEndpointURL:
    Description: URL of your API endpoint
    Value:
      'Fn::Sub': >-
        https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/news/



Cloud9 Code 창 하단의 Terminal로 접근

  1. 정적 웹 호스팅 파일 다운로드 받기

    코드 블럭
    wget https://s3.ap-northeast-2.amazonaws.com/polly.awsdemokr.com/301_static_web.zip


  2. 축 풀고 폴더 이동

    코드 블럭
    unzip 301_static_web.zip
    cd 301_static_web


  3. Cloud9에서 scripts.js 파일 열어서 CloudFormation Stack에 배포된 Output의 APIEndpointURL 값을 소스코드에 반영 (WebsiteURL이 아니므로 주의)

    코드 블럭
    var API_ENDPOINT = "https://xxxxxxxxxx.execute-api.ap-southeast-1.amazonaws.com/Prod/news/";
    if (API_ENDPOINT === "")
    {
            alert("scripts.js 파일의 상단에 API Gateway에 배포한 URL을 등록하고 실행하세요.");
    }


  4. 정적 웹 포스팅하고자 하는 S3 버킷에 public-read 권한으로 파일을 업로드 (CloudFormation Stack에 배포된 Output의 S3WebBucket 값을 아래에 대체)

    코드 블럭
    aws s3 sync . s3://cloud9-webapp-staticwebbucket-xxxxxxxxxxxx --acl public-read


  5. 웹 브라우저로 정적 웹 페이지에 접속 (CloudFormation Stack에 배포된 Output의 WebsiteURL 값을 웹 브라우저 주소창에 입력)
    1. https://cloud9-webapp-staticwebbucket-xxxxxxxxxxxx.s3.amazonaws.com

웹 페이지 동작 확인

  1. 텍스트를 등록
  2. 검색
  3. 재생
  4. 텍스트를 옵션을 주어서 등록
  5. 검색
  6. 재생
  7. 삭제

CloudWatch Logs를 통해서 로그를 확인


기타 방법:

 https://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/DynamoDBLocal.html


압축 풀기


스크립 파일 수정하기


정적 웹 호스팅 파일 S3에 업로드 하기

...