【AWS CLI Samples】Lambda

Lambda

The execution of commands is primarily verified in AWS Cloud9.
If you would like to provide command examples, please submit them through the contact form.
You may also use this form to request corrections for the listed command examples.


Get list of functions

aws lambda list-functions \
--query 'Functions[*].[Runtime, FunctionName] | map(&[], @)' \
--output text

output

python3.12      FunctionA
FunctionB-fromECR
python3.12      FunctionC
<以下省略>

by anonymous

Get the code location (URL) of a function

aws lambda get-function \
--function-name FunctionA \
--query 'Code.Location' \
--output text

output

https://awslambda-ap-ne-1-tasks.s3.ap-northeast-1.amazonaws.com/snapshots/123456789012/FunctionA-0123456-aaaa-bbbb-cccc-0123456789?versionId=6rVz02wc9INE2oEsrk2HkEw8iCFMacSA&X-Amz-Security-Token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240518T223457Z&X-Amz-SignedHeaders=host&X-Amz-Expires=600&X-Amz-Credential=YYYYYYYYYYYYYYYYYYYYYYYYY&X-Amz-Signature=ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

by anonymous

Get the code of a function

url=$(aws lambda get-function \
--function-name FunctionA \
--query 'Code.Location' \
--output text)
curl -s -o code.zip $url
unzip code.zip
rm code.zip
cat lambda_function.py

output

import json
import boto3
import datetime

def lambda_handler(event, context):
    
    time = datetime.datetime.fromtimestamp(event['timestamp'])
    name = event['name']
    val  = event['value']
    mrx  = event['metric']

    try:
        cw = boto3.client("cloudwatch")
        cw.put_metric_data(
            Namespace="Custom",
            MetricData=[
                {
                    "MetricName": mrx,
                    "Timestamp": time,
                    "Value": val,
                    "Dimensions": [
                        {
                            "Name": "CustomName",
                            "Value": name
                        }
                    ],
                    "Unit": "Count"
                },
            ]
        )

    except Exception as e:
        print("Error:", str(e))
        raise e

    return {
        'statusCode': 200,
        'body': json.dumps('Put cw metric datapoint')
    }

by anonymous

タイトルとURLをコピーしました