Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Menangani ECS acara Amazon
Amazon ECS mengirimkan acara setidaknya sekali. Ini berarti Anda mungkin menerima banyak salinan dari acara tertentu. Selain itu, peristiwa mungkin tidak dikirimkan ke listener peristiwa Anda sesuai urutan peristiwa yang terjadi.
Untuk memesan acara dengan benar, detail
bagian dari setiap acara berisi version
properti. Setiap kali sumber daya mengubah status, version
ini bertambah. Peristiwa duplikat memiliki version
yang sama di objek detail
. Jika Anda mereplikasi instans ECS penampung Amazon dan status tugas dengan EventBridge, Anda dapat membandingkan versi sumber daya yang dilaporkan oleh Amazon ECS APIs dengan sumber daya yang version
dilaporkan dalam EventBridge untuk memverifikasi bahwa versi dalam aliran peristiwa Anda saat ini. Peristiwa dengan jumlah properti versi yang lebih tinggi harus diperlakukan sebagai peristiwa yang terjadi setelah peristiwa dengan jumlah versi yang lebih rendah.
Contoh: Menangani peristiwa di fungsi AWS Lambda
Contoh berikut menunjukkan fungsi Lambda yang ditulis dengan Python 3.9 yang menangkap peristiwa perubahan status instance tugas dan kontainer dan menyimpannya ke salah satu dari dua tabel Amazon DynamoDB:
-
ECSCtrInstanceState— Menyimpan status terbaru untuk instance kontainer. ID tabel adalah nilai
containerInstanceArn
instans kontainer. -
ECSTaskState— Menyimpan status terbaru untuk suatu tugas. ID tabel adalah nilai
taskArn
tugas.
import json import boto3 def lambda_handler(event, context): id_name = "" new_record = {} # For debugging so you can see raw event format. print('Here is the event:') print((json.dumps(event))) if event["source"] != "aws.ecs": raise ValueError("Function only supports input from events with a source type of: aws.ecs") # Switch on task/container events. table_name = "" if event["detail-type"] == "ECS Task State Change": table_name = "ECSTaskState" id_name = "taskArn" event_id = event["detail"]["taskArn"] elif event["detail-type"] == "ECS Container Instance State Change": table_name = "ECSCtrInstanceState" id_name = "containerInstanceArn" event_id = event["detail"]["containerInstanceArn"] else: raise ValueError("detail-type for event is not a supported type. Exiting without saving event.") new_record["cw_version"] = event["version"] new_record.update(event["detail"]) # "status" is a reserved word in DDB, but it appears in containerPort # state change messages. if "status" in event: new_record["current_status"] = event["status"] new_record.pop("status") # Look first to see if you have received a newer version of an event ID. # If the version is OLDER than what you have on file, do not process it. # Otherwise, update the associated record with this latest information. print("Looking for recent event with same ID...") dynamodb = boto3.resource("dynamodb", region_name="us-east-1") table = dynamodb.Table(table_name) saved_event = table.get_item( Key={ id_name : event_id } ) if "Item" in saved_event: # Compare events and reconcile. print(("EXISTING EVENT DETECTED: Id " + event_id + " - reconciling")) if saved_event["Item"]["version"] < event["detail"]["version"]: print("Received event is a more recent version than the stored event - updating") table.put_item( Item=new_record ) else: print("Received event is an older version than the stored event - ignoring") else: print(("Saving new event - ID " + event_id)) table.put_item( Item=new_record )
Contoh Fargate berikut menunjukkan fungsi Lambda yang ditulis dengan Python 3.9 yang menangkap peristiwa perubahan status tugas dan menyimpannya ke tabel Amazon DynamoDB berikut:
import json import boto3 def lambda_handler(event, context): id_name = "" new_record = {} # For debugging so you can see raw event format. print('Here is the event:') print((json.dumps(event))) if event["source"] != "aws.ecs": raise ValueError("Function only supports input from events with a source type of: aws.ecs") # Switch on task/container events. table_name = "" if event["detail-type"] == "ECS Task State Change": table_name = "ECSTaskState" id_name = "taskArn" event_id = event["detail"]["taskArn"] else: raise ValueError("detail-type for event is not a supported type. Exiting without saving event.") new_record["cw_version"] = event["version"] new_record.update(event["detail"]) # "status" is a reserved word in DDB, but it appears in containerPort # state change messages. if "status" in event: new_record["current_status"] = event["status"] new_record.pop("status") # Look first to see if you have received a newer version of an event ID. # If the version is OLDER than what you have on file, do not process it. # Otherwise, update the associated record with this latest information. print("Looking for recent event with same ID...") dynamodb = boto3.resource("dynamodb", region_name="us-east-1") table = dynamodb.Table(table_name) saved_event = table.get_item( Key={ id_name : event_id } ) if "Item" in saved_event: # Compare events and reconcile. print(("EXISTING EVENT DETECTED: Id " + event_id + " - reconciling")) if saved_event["Item"]["version"] < event["detail"]["version"]: print("Received event is a more recent version than the stored event - updating") table.put_item( Item=new_record ) else: print("Received event is an older version than the stored event - ignoring") else: print(("Saving new event - ID " + event_id)) table.put_item( Item=new_record )