[AWS] Lab: CloudFormation and SAM to deploy a Serverless fuction
Task Breakdown
- Install the AWS SAM CLI on your local machine
- Create an S3 bucket
- Use `sam package` to package our code and save it in S3
- Use `sam deploy` to deploy a Lambda function using CloudFormation
Install SAM CLI
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.
- It create bucket on S3 and save the CloudFormation template
- sam package & sam deploy cmds.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-04-09 [Spring Boot] Use Component Scan to scan for Bean
2019-04-09 [Docker] Docker compose in action for Node.js and MongoDb
2019-04-09 [Docker] Docker Compose basic
2018-04-09 [Performance] Optimize Paint and Composite for the website
2017-04-09 [Angular] Implementing a ControlValueAccessor