基于Cloud9部署Lambda:项目结构与SAM模板配置咨询
Got it, let's walk through how to get your Lambda project deployed using Cloud9 step by step. Your project structure and SAM template are already set up nicely, so we can focus on the deployment workflow.
1. Prep Your Cloud9 Environment
First, make sure your Cloud9 environment is ready to handle SAM deployments:
- Check Node.js version: Run
node --versionin the Cloud9 terminal. While your Lambda usesnodejs6.10(note: this runtime is end-of-life—more on that later), your Cloud9 environment can use a newer Node version since SAM just packages the code. - Install/Verify SAM CLI: Run
sam --version. If it's not installed, install it via pip:
Restart your terminal if needed to ensure SAM CLI is added to your PATH.pip install aws-sam-cli --user
2. Configure AWS Credentials
Cloud9 typically links to your AWS IAM role automatically, but if you need to set credentials manually:
- Run
aws configurein the terminal, then input your:- AWS Access Key ID
- AWS Secret Access Key
- Default region (match the region you want to deploy your Lambda to)
- Default output format (e.g.,
json)
- Verify your setup works with:
You should see your IAM user/role details if everything's correct.aws sts get-caller-identity
3. (Optional) Test Locally First
Before deploying to AWS, it's a good idea to test your functions locally to catch issues early:
- Create a test event file (e.g.,
event.json) in your project root with sample input for your Lambda. - Invoke Func1 locally:
This helps you spot code bugs or path errors (like incorrect references tosam local invoke Func1 -e event.jsoncommon.js) before pushing to AWS.
4. Package and Deploy Your Project
SAM needs to package your code and upload it to an S3 bucket before deploying to Lambda:
Step 4.1: Create an S3 Bucket (if you don't have one)
You need an S3 bucket in the same region as your deployment. Create one with:
aws s3 mb s3://your-unique-bucket-name --region us-east-1
Replace your-unique-bucket-name with a unique name and us-east-1 with your target region.
Step 4.2: Package the Project
Run this command to package your code and generate a packaged SAM template:
sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket your-unique-bucket-name
This uploads your function code to the S3 bucket and updates packaged.yaml with the correct S3 paths.
Step 4.3: Deploy to AWS
Finally, deploy the packaged template to create your Lambda stack:
sam deploy --template-file packaged.yaml --stack-name your-lambda-stack-name --capabilities CAPABILITY_IAM
- Replace
your-lambda-stack-namewith a name for your CloudFormation stack. - The
CAPABILITY_IAMflag is required because SAM creates IAM roles for your Lambda functions.
5. Verify Deployment
After deployment completes, confirm your Lambda is live:
- List your Lambda functions to check Func1 exists:
aws lambda list-functions --query "Functions[?FunctionName=='Func1']" - Or use the AWS Explorer in Cloud9 to navigate to Lambda and view your deployed function directly.
Key Notes to Keep in Mind
- Runtime Update:
nodejs6.10is no longer supported by AWS. For security and compatibility, update theRuntimefield in yourtemplate.yamlto a supported version likenodejs20.x. - Common Module References: Ensure your
Func1/index.jsandFunc2/index.jscorrectly referencecommon.jswith a relative path, e.g.:
SAM will include this file in the deployment package as long as the path is correct.const common = require('../Common/common.js'); - Additional Config: If you need environment variables, layers, or other Lambda settings, add them to the
Propertiessection of your function intemplate.yaml.
内容的提问来源于stack exchange,提问作者Yevhen

