본문 바로가기
Cloud/AWS

[2021.06.22] 인턴 +113 How to deploy SAM Application in VS Code?

by injekim97 2021. 6. 22.
반응형

[2021.06.22] 인턴 +113 How to deploy SAM Application in VS Code?

 

 

 

이번 게시글은 드디어 VS Code에서, Lambda를 배포하기 위한 내용을 작성하려고 한다.

우선, 해당 아래의 링크를 클릭하여, Setting을 해준 후, 이번 게시글의 내용을 차례대로 수행하면 된다.

 

* 참고한 동영상 주소

-> Deploy a Serverless Application Model (SAM) Application with the AWS Toolkit for VS Code - YouTube

 

 

 

[2021.06.17] 인턴 +108 How to set up git client for code commit operations (AWS, +@ AWS IAM LOGIN) :: 기초부터 다지는

 

injekim97.tistory.com

 

 

 

[2021.06.18~21] 인턴 +109 How to Connect AWS Lambda in VS code(AWS toolkit)

[2021.06.18~21] 인턴 +109 How to Connect AWS Lambda in VS code(AWS toolkit) 오늘은, 서버리스를 위해, AWS Lambda 기능을 사용해야하는데, AWS Toolkit을 사용해야 한다. AWS toolkit 사용법 과..

injekim97.tistory.com

 

 

참고로, IAM 게정을 만들어서 권한을 부여 해줬기 때문에, 개인에게 권한을 추가하는 것 보다 그룹을 만들어서 권한을 부여하는 것을 무조건!!!! 추천한다.

 

* Reason?

-> 개인으로 권한을 추가할 수 있는 것은 10개로 지정되어있다. 권한 갯수를 늘리기 위해 Search를 하였지만, 실패

그렇기 때문에, 꼭 한 그룹에 권한을 10개 설정하여, 추가해서 권한을 부여할 것 

----------------------------------------------------------------------------------------------------------------------------------

<How to deploy SAM Application>

VS Code에서 컨트롤 + 쉬프트 + P 를 눌러 빨간색 으로 칠한 것들을 쳐주면서 순차적으로 진행할 것(1~4)


>AWS: CREATE credentials profile
-> IAM 만든 아이디 , ACCESS_KEY, SECRET_KEY 입력


>AWS: Connect to AWS
-> IAM 계정으로 connect



>AWS: Create Lambda SAM Application
-> version : Python 3.8
-> AWS SAM Hello World
-> 만든 Lambda를 지정할 폴더(경로) 지정
-> Lambda name 지정



>aws deploy SAM Application
-> 템플릿 지정(앞에서 만든 Lambda 로 지정)
-> region 지정(배포할 지역을 설정)
-> S3 Bucket 지정
-> 앞에서 지정한 S3 Bucket이름과 똑같이 지정해줌

 

 

 

--------------------------------------------------------------------------------------------------------------------------------

<SAM Application Output>

-> log를 읽어보면, error에 권한이 없다고 하는데, 그럴 경우에는 IAM을 만든 Root 게정으로 들어가서, 해당 error의 권한을 부여해주면 됨

2021-06-22 16:21:24 [INFO]: Starting SAM Application deployment...
2021-06-22 16:21:24 [INFO]: Building SAM Application...
2021-06-22 16:21:25 [INFO]: Running command: (not started) [C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd build --build-dir C:\Users\monorama\AppData\Local\Temp\aws-toolkit-vscode\samDeploywVIzSa\build --template c:\temp-python\lambda-python3.8\template.yaml]
2021-06-22 16:21:29 [INFO]: Building codeuri: c:\temp-python\lambda-python3.8\hello_world runtime: python3.8 metadata: {} functions: ['HelloWorldFunction']

2021-06-22 16:21:29 [INFO]: Running PythonPipBuilder:ResolveDependencies

2021-06-22 16:21:34 [INFO]: Running PythonPipBuilder:CopySource

2021-06-22 16:21:34 [INFO]: 
Build Succeeded

2021-06-22 16:21:34 [INFO]: 
Built Artifacts  : ..\..\Temp\aws-toolkit-vscode\samDeploywVIzSa\build
Built Template   : ..\..\Temp\aws-toolkit-vscode\samDeploywVIzSa\build\template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t ..\..\Temp\aws-toolkit-vscode\samDeploywVIzSa\build\template.yaml
[*] Deploy: sam deploy --guided --template-file ..\..\Temp\aws-toolkit-vscode\samDeploywVIzSa\build\template.yaml
    

2021-06-22 16:21:34 [INFO]: Packaging SAM Application to S3 Bucket: 0622testbucket
2021-06-22 16:21:36 [INFO]: Running command: (not started) [C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd package --template-file C:\Users\monorama\AppData\Local\Temp\aws-toolkit-vscode\samDeploywVIzSa\build\template.yaml --s3-bucket 0622testbucket --output-template-file C:\Users\monorama\AppData\Local\Temp\aws-toolkit-vscode\samDeploywVIzSa\template.yaml --region ap-northeast-2]
2021-06-22 16:21:42 [INFO]: 
Uploading to eb6b1b36894a4e73ebf0e12b7a642d9d  262144 / 583203  (44.95%)
2021-06-22 16:21:42 [INFO]: 
Uploading to eb6b1b36894a4e73ebf0e12b7a642d9d  524288 / 583203  (89.90%)
2021-06-22 16:21:42 [INFO]: 
Uploading to eb6b1b36894a4e73ebf0e12b7a642d9d  583203 / 583203  (100.00%)
2021-06-22 16:21:42 [INFO]: 
Successfully packaged artifacts and wrote output template to file C:\Users\monorama\AppData\Local\Temp\aws-toolkit-vscode\samDeploywVIzSa\template.yaml.
Execute the following command to deploy the packaged template
sam deploy --template-file C:\Users\monorama\AppData\Local\Temp\aws-toolkit-vscode\samDeploywVIzSa\template.yaml --stack-name <YOUR STACK NAME>


2021-06-22 16:21:43 [INFO]: 

2021-06-22 16:21:43 [INFO]: Deploying SAM Application to CloudFormation Stack: testbucket
2021-06-22 16:21:44 [INFO]: Running command: (not started) [C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd deploy --template-file C:\Users\monorama\AppData\Local\Temp\aws-toolkit-vscode\samDeploywVIzSa\template.yaml --stack-name testbucket --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND --region ap-northeast-2 --s3-bucket 0622testbucket]
2021-06-22 16:21:49 [INFO]: 
	Deploying with following values
	===============================
	Stack name                   : testbucket
	Region                       : ap-northeast-2
	Confirm changeset            : False
	Deployment s3 bucket         : 0622testbucket
	Capabilities                 : ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]

2021-06-22 16:21:49 [INFO]: 	Parameter overrides          : {}
	Signing Profiles             : {}

Initiating deployment
=====================

2021-06-22 16:21:49 [INFO]: 
Uploading to 0201be8e1d96d4585e5366725e066356.template  1094 / 1094  (100.00%)
2021-06-22 16:21:50 [INFO]: 
Waiting for changeset to be created..

2021-06-22 16:21:55 [INFO]: 
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------

2021-06-22 16:21:55 [INFO]: Operation                LogicalResourceId        ResourceType             Replacement            
-------------------------------------------------------------------------------------------------

2021-06-22 16:21:56 [INFO]: + Add                    HelloWorldFunctionHell   AWS::Lambda::Permissio   N/A                    
                         oWorldPermissionProd     n                                               

2021-06-22 16:21:56 [INFO]: + Add                    HelloWorldFunctionRole   AWS::IAM::Role           N/A                    
+ Add                    HelloWorldFunction       AWS::Lambda::Function    N/A                    
+ Add                    ServerlessRestApiDeplo   AWS::ApiGateway::Deplo   N/A                    
                         yment47fc2d5f9d          yment                                           
+ Add                    ServerlessRestApiProdS   AWS::ApiGateway::Stage   N/A                    
                         tage                                                                     
+ Add                    ServerlessRestApi        AWS::ApiGateway::RestA   N/A                    
                                                  pi                                              
-------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:ap-northeast-2:187928013240:changeSet/samcli-deploy1624346509/e398f6b9-0c55-452a-8f68-1e0865359802


2021-06-22 16:21:56 [INFO]: 
2021-06-22 16:21:56 - Waiting for stack create/update to complete

2021-06-22 16:21:56 [INFO]: 
CloudFormation events from changeset
-------------------------------------------------------------------------------------------------
ResourceStatus           ResourceType             LogicalResourceId        ResourceStatusReason   

2021-06-22 16:21:56 [INFO]: -------------------------------------------------------------------------------------------------

2021-06-22 16:22:00 [INFO]: CREATE_IN_PROGRESS       AWS::IAM::Role           HelloWorldFunctionRole   -                      

2021-06-22 16:22:00 [INFO]: CREATE_IN_PROGRESS       AWS::IAM::Role           HelloWorldFunctionRole   Resource creation      
                                                                           Initiated              

2021-06-22 16:22:22 [INFO]: CREATE_COMPLETE          AWS::IAM::Role           HelloWorldFunctionRole   -                      

2021-06-22 16:22:25 [INFO]: CREATE_IN_PROGRESS       AWS::Lambda::Function    HelloWorldFunction       -                      

2021-06-22 16:22:28 [INFO]: CREATE_IN_PROGRESS       AWS::Lambda::Function    HelloWorldFunction       Resource creation      

2021-06-22 16:22:28 [INFO]:                                                                            Initiated              

2021-06-22 16:22:28 [INFO]: CREATE_COMPLETE          AWS::Lambda::Function    HelloWorldFunction       -                      

2021-06-22 16:22:31 [INFO]: CREATE_IN_PROGRESS       AWS::ApiGateway::RestA   ServerlessRestApi        -                      
                         pi                                                                       

2021-06-22 16:22:33 [INFO]: CREATE_IN_PROGRESS       AWS::ApiGateway::RestA   ServerlessRestApi        Resource creation      
                         pi                                                Initiated              

2021-06-22 16:22:34 [INFO]: CREATE_COMPLETE          AWS::ApiGateway::RestA   ServerlessRestApi        -                      
                         pi                                                                       

2021-06-22 16:22:35 [INFO]: CREATE_IN_PROGRESS       AWS::Lambda::Permissio   HelloWorldFunctionHell   -                      
                         n                        oWorldPermissionProd                            

2021-06-22 16:22:36 [INFO]: CREATE_IN_PROGRESS       AWS::ApiGateway::Deplo   ServerlessRestApiDeplo   -                      
                         yment                    yment47fc2d5f9d                                 

2021-06-22 16:22:36 [INFO]: CREATE_IN_PROGRESS       AWS::Lambda::Permissio   HelloWorldFunctionHell   Resource creation      
                         n                        oWorldPermissionProd     Initiated              

2021-06-22 16:22:36 [INFO]: CREATE_IN_PROGRESS       AWS::ApiGateway::Deplo   ServerlessRestApiDeplo   Resource creation      
                         yment                    yment47fc2d5f9d          Initiated              

2021-06-22 16:22:37 [INFO]: CREATE_COMPLETE          AWS::ApiGateway::Deplo   ServerlessRestApiDeplo   -                      
                         yment                    yment47fc2d5f9d                                 

2021-06-22 16:22:38 [INFO]: CREATE_IN_PROGRESS       AWS::ApiGateway::Stage   ServerlessRestApiProdS   -                      
                                                  tage                                            

2021-06-22 16:22:39 [INFO]: CREATE_IN_PROGRESS       AWS::ApiGateway::Stage   ServerlessRestApiProdS   Resource creation      
                                                  tage                     Initiated              

2021-06-22 16:22:40 [INFO]: CREATE_COMPLETE          AWS::ApiGateway::Stage   ServerlessRestApiProdS   -                      
                                                  tage                                            

2021-06-22 16:22:46 [INFO]: CREATE_COMPLETE          AWS::Lambda::Permissio   HelloWorldFunctionHell   -                      
                         n                        oWorldPermissionProd                            

2021-06-22 16:22:49 [INFO]: CREATE_COMPLETE          AWS::CloudFormation::S   testbucket               -                      
                         tack                                                                     

2021-06-22 16:22:49 [INFO]: -------------------------------------------------------------------------------------------------

2021-06-22 16:22:49 [INFO]: 
CloudFormation outputs from deployed stack
-------------------------------------------------------------------------------------------------
Outputs                                                                                         
-------------------------------------------------------------------------------------------------

2021-06-22 16:22:49 [INFO]: Key                 HelloWorldFunctionIamRole                                                   
Description         Implicit IAM Role created for Hello World function                          
Value               arn:aws:iam::187928013240:role/testbucket-                                  
HelloWorldFunctionRole-15AEYJR3JIFV2                                                            

Key                 HelloWorldApi                                                               
Description         API Gateway endpoint URL for Prod stage for Hello World function            
Value               https://7tr3na9qik.execute-api.ap-northeast-2.amazonaws.com/Prod/hello/     

Key                 HelloWorldFunction                                                          
Description         Hello World Lambda Function ARN                                             
Value               arn:aws:lambda:ap-northeast-2:187928013240:function:testbucket-             
HelloWorldFunction-sHaeAAqt56cV                                                                 
-------------------------------------------------------------------------------------------------

Successfully created/updated stack - testbucket in ap-northeast-2


2021-06-22 16:22:50 [INFO]: 

2021-06-22 16:22:50 [INFO]: Successfully deployed SAM Application to CloudFormation Stack: testbucket

 

 

 

이제, 잘 동작하는지 확인해보자. 

1. AWS -> Lambda 페이지에 들어간다.

-> 형광펜으로 밑줄 친 것이, SAM Application deploy 로 만들어 졌다.

 

 

2. Lambda -> 오른쪽 마우스 눌러서 Invoke on AWS 클릭

* 형광펜으로 밑줄 친 것이, SAM Application deploy 로 만들어 졌다.

 

 

-> Invoke 클릭

 

 

 

-> 정상적으로 작동 되었다.

 

반응형

댓글