Resources: HelloWorldFunction: Type:AWS::Serverless::Function# More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: CodeUri:hello-world/ Handler:app.lambdaHandler Runtime:nodejs14.x Layers: -'arn:aws:lambda:us-east-1:553035198032:layer:git-lambda2:8'# 여기를 추가!!
sam template.yaml에서는 위와 같이 추가한다
또는 aws console로 직접 해당 람다에 가서 추가해 줄 수 있다
람다에서 깃 쓸 때 고려사항
람다에서는 가급적 가벼운 일을 해야 한다
git clone 한다면 /tmp 디렉터리에서 한다 (clone 옵션을 살펴서 최대한 필요한 것만 가져오자)
인증은 Personal access tokens을 사용했다
토큰이 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 이렇게 있을 때,
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: Function: Timeout:3
Resources: HelloWorldFunction: Type:AWS::Serverless::Function# More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: Layers: -!RefDependencyLayer CodeUri:hello-world/ Handler:app.lambdaHandler Runtime:nodejs12.x Events: HelloWorld: Type:Api# More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api Properties: Path:/hello Method:get DependencyLayer: Type:AWS::Serverless::LayerVersion Properties: LayerName:HelloWorldFunction-layer Description:DependenciesforHelloWorldFunction ContentUri:opt/ CompatibleRuntimes: -nodejs12.x RetentionPolicy:Retain
Outputs: # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function # Find out more about other implicit resources you can reference within SAM # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api 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:!GetAttHelloWorldFunction.Arn HelloWorldFunctionIamRole: Description:'Implicit IAM Role created for Hello World function' Value:!GetAttHelloWorldFunctionRole.Arn
최종코드는 이러하다
눈여겨 봐야할 것은
Resources아래에 DependencyLayer라는 이름으로 layer정의하고
ContentUri: opt/ 를 지정한다. 이 opt 디렉터리에 우리가 layer로 따로뺄 패키지가 들어가게 될것이다
Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: test-sam-app AWS Region [us-east-1]: ap-northeast-2 #Shows you resources changes to be deployed and require a 'Y' to initiate deploy Confirm changes before deploy [y/N]: y #SAM needs permission to be able to create roles to connect to the resources in your template Allow SAM CLI IAM role creation [Y/n]: y HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y Save arguments to samconfig.toml [Y/n]: y
Changeset created successfully. arn:aws:cloudformation:ap-northeast-2:111111111111:changeSet/samcli-deploy1600652952/89cf8cb3-a626-44ef-bd26-815dacedaa8e
Previewing CloudFormation changeset before deployment ====================================================== Deploy this changeset? [y/N]: y
또 어쩌구 저쩌구 나오면서 마지막 확인을 받는데 y 해준다
cmd log
1
Successfully created/updated stack - test-sam-app in ap-northeast-2
그러면 시간이 쫌 걸리면서 aws에 올라가게 된다
또 samconfig.toml 파일이 생성된 것을 확인할 수 있다
이제 브라우저를 열어서 aws lambda 로 들어간다
위와 같이 [스택네임]-HelloWorldFunction이름의 함수 하나가 생성되었다
눌러서 들어간다
이런 화면이 나온다
api 게이트웨이를 클릭한다. 그러면 아래 화면이 api 게이트웨이로 바뀐다. 거기서 다시 세부 정보를 클릭해서 api 엔드포인트를 확인한다
근데 우리는 {“message”:”hello world”} 만 보이니까 body에 있는 내용을 조작하면 우리가 원하는 내용을 보낼 수 있겠구나 생각할 수 있다
그러면 이 함수를 실행시키는 트리거, s22xid3g26.execute-api.ap-northeast-2.amazonaws.com/Prod/hello
api 게이트웨이의 엔드포인트는 어디서 설정되었을까?
template.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13
Resources: HelloWorldFunction: Type:AWS::Serverless::Function# More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: CodeUri:hello-world/ Handler:app.lambdaHandler Runtime:nodejs12.x Events: HelloWorld: Type:Api# More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api Properties: Path:/hello Method:get