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

如何为AWS从GitHub执行的代码添加定时器及Lambda问题解决

Hey there, let's walk through your questions and the Lambda issue you resolved, with some additional context to help you out:

Questions About AWS Systems Manager & GitHub Execution

1. Can I add a timer to run the script on a schedule?

Absolutely! You can pair AWS Systems Manager (SSM) Automation with Amazon EventBridge (formerly CloudWatch Events) to schedule your script execution. Here's how it works:

  • First, create an SSM Automation document that defines pulling your Python script from GitHub and executing it (using the payload you provided: {"owner":"owner_name", "repository": "repository_name", "path": "path_to_scripts_or_directory", "tokenInfo":"{{ssm-secure:SecureString_parameter_name}}" }).
  • Then, set up an EventBridge rule with a schedule expression (either cron for specific times, e.g., cron(0 12 * * ? *) for daily at noon UTC, or rate for intervals like rate(1 day)).
  • Configure the EventBridge rule's target to be your SSM Automation document. This will trigger the automation on your specified schedule.

2. Can I pass an API key in the code to avoid configuring credentials elsewhere?

While technically possible, hardcoding or passing API keys directly in your code is not secure (it risks exposure in logs, version control, etc.). Instead, here are better approaches:

  • Use AWS SSM Parameter Store's SecureString type to store your API key (this is what your tokenInfo is already referencing). Your script can then use the AWS SDK (boto3) to fetch the key from Parameter Store at runtime, without embedding it in code.
  • If you need to pass the key as a parameter to your script, you can define input parameters in your SSM Automation document, reference the SecureString parameter there, and pass it to your Python script as a command-line argument or environment variable.

Lambda Deployment Troubleshooting

You mentioned hitting the error {"errorMessage": "Handler 'handler' missing on module 'AWS_Backup'"} when deploying your script as a Lambda function, and fixed it by updating the handler and adding IAM permissions. Let's clarify why that error happened and reinforce the solution:

Why the error occurred

Lambda uses the handler configuration to know which function to execute in your code. The format is filename.function_name—you initially set it to AWS_Backup.handler, but your code's entrypoint function is named lambda_handler, so Lambda couldn't find the matching function.

Fixed code reference

Here's your corrected Lambda code (with the missing boto3 import added, which you likely had in your actual code):

from __future__ import print_function
import boto3

def lambda_handler(event, context):
    SOURCE_REGION = 'us-east-2'
    DESTINATION_REGION = 'us-east-1'
    
    # Connect to EC2 in Source region
    source_client = boto3.client('ec2', region_name=SOURCE_REGION)
    
    # Get a list of all snapshots, then sort them
    snapshots = source_client.describe_snapshots(OwnerIds=['self'])
    snapshots_sorted = sorted([(s['SnapshotId'], s['StartTime']) for s in snapshots['Snapshots']], key=lambda k: k[1])
    latest_snapshot = snapshots_sorted[-1][0]
    print ('Latest Snapshot ID is ' + latest_snapshot)
    
    # Connect to EC2 in Destination region
    destination_client = boto3.client('ec2', region_name=DESTINATION_REGION)
    
    # Copy the snapshot
    response = destination_client.copy_snapshot(
        SourceSnapshotId=latest_snapshot,
        SourceRegion=SOURCE_REGION,
        Description='This is my copied snapshot'
    )
    print ('Copied Snapshot ID is ' + response['SnapshotId'])

Key fixes you implemented

  • Updated the Lambda handler to AWS_Backup.lambda_handler to match the function name in your code.
  • Attached an IAM role to the Lambda function with the necessary permissions:
    • ec2:DescribeSnapshots to list your snapshots in the source region.
    • ec2:CopySnapshot to copy the snapshot to the destination region.
    • Basic Lambda execution permissions (e.g., AWSLambdaBasicExecutionRole) to allow Lambda to log to CloudWatch.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:28:48