You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Cloud9部署Lambda:项目结构与SAM模板配置咨询

Deploying Your Lambda Project via Cloud9 (Using AWS 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 --version in the Cloud9 terminal. While your Lambda uses nodejs6.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:
    pip install aws-sam-cli --user
    
    Restart your terminal if needed to ensure SAM CLI is added to your PATH.

2. Configure AWS Credentials

Cloud9 typically links to your AWS IAM role automatically, but if you need to set credentials manually:

  • Run aws configure in 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:
    aws sts get-caller-identity
    
    You should see your IAM user/role details if everything's correct.

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:
    sam local invoke Func1 -e event.json
    
    This helps you spot code bugs or path errors (like incorrect references to common.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-name with a name for your CloudFormation stack.
  • The CAPABILITY_IAM flag 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.10 is no longer supported by AWS. For security and compatibility, update the Runtime field in your template.yaml to a supported version like nodejs20.x.
  • Common Module References: Ensure your Func1/index.js and Func2/index.js correctly reference common.js with a relative path, e.g.:
    const common = require('../Common/common.js');
    
    SAM will include this file in the deployment package as long as the path is correct.
  • Additional Config: If you need environment variables, layers, or other Lambda settings, add them to the Properties section of your function in template.yaml.

内容的提问来源于stack exchange,提问作者Yevhen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:14:16