{"id":1342,"library":"aws-cdk-asset-awscli-v1","title":"AWS CDK Asset AWS CLI v1","description":"The `aws-cdk-asset-awscli-v1` library provides the AWS CLI (version 1) as a Lambda Layer asset for use within AWS CDK applications. It allows Lambda functions to execute AWS CLI commands directly. This package is maintained by cdklabs and receives updates in sync with the broader AWS CDK ecosystem.","status":"active","version":"2.2.274","language":"en","source_language":"en","source_url":"https://github.com/cdklabs/awscdk-asset-awscli.git","tags":["aws","cdk","lambda","awscli","asset","layer","cloud"],"install":[{"cmd":"pip install aws-cdk-asset-awscli-v1 aws-cdk-lib","lang":"bash","label":"Install library and AWS CDK"}],"dependencies":[],"imports":[{"note":"The `AwsCliLayer` from this package is a separate asset construct, not directly part of `aws-cdk-lib`'s core `aws_lambda` module.","wrong":"from aws_cdk.aws_lambda import AwsCliLayer","symbol":"AwsCliLayer","correct":"from aws_cdk_asset_awscli_v1 import AwsCliLayer"}],"quickstart":{"code":"import os\nfrom aws_cdk import (\n    App,\n    Stack,\n    aws_lambda as lambda_,\n)\nfrom aws_cdk_asset_awscli_v1 import AwsCliLayer\n\nclass MyAwsCliStack(Stack):\n    def __init__(self, scope: App, construct_id: str, **kwargs) -> None:\n        super().__init__(scope, construct_id, **kwargs)\n\n        # Create the AWS CLI v1 Lambda Layer\n        aws_cli_layer = AwsCliLayer(self, \"AwsCliV1Layer\")\n\n        # Define a Lambda function that uses the layer\n        lambda_function = lambda_.Function(\n            self,\n            \"MyLambdaWithAwsCli\",\n            runtime=lambda_.Runtime.PYTHON_3_9, # Ensure compatibility with asset's Python requirement\n            handler=\"index.handler\",\n            code=lambda_.Code.from_inline(\n                \"\"\"\nimport json\nimport subprocess\n\ndef handler(event, context):\n    try:\n        # Example: run 'aws s3 ls' to list S3 buckets\n        # The Lambda's IAM role must have s3:ListBucket permissions.\n        result = subprocess.run(['aws', 's3', 'ls'], capture_output=True, text=True, check=True)\n        return {\n            'statusCode': 200,\n            'body': json.dumps({'message': 'AWS CLI executed successfully', 'output': result.stdout})\n        }\n    except subprocess.CalledProcessError as e:\n        return {\n            'statusCode': 500,\n            'body': json.dumps({'error': f\"AWS CLI error: {e.stderr}\"})\n        }\n    except Exception as e:\n        return {\n            'statusCode': 500,\n            'body': json.dumps({'error': str(e)})\n        }\n                \"\"\"\n            ),\n            layers=[aws_cli_layer],\n            # Add IAM permissions required for CLI commands, e.g., s3:ListBucket for 'aws s3 ls'\n            # For a production setup, consider defining a more granular policy.\n            # lambda_function.add_to_role_policy(iam.PolicyStatement(actions=[\"s3:ListBucket\"], resources=[\"arn:aws:s3:::*\"]))\n        )\n\napp = App()\nMyAwsCliStack(app, \"MyAwsCliStack\")\napp.synth()","lang":"python","description":"This quickstart demonstrates how to create an `AwsCliLayer` and attach it to a Lambda function. The Lambda's code then executes a simple AWS CLI command (`aws s3 ls`). Remember to grant the Lambda's execution role the necessary IAM permissions for any AWS CLI commands it will run."},"warnings":[{"fix":"Verify the required AWS CLI version. If v2 is needed, install `aws-cdk-asset-awscli-v2` and import `AwsCliLayer` from there.","message":"This package specifically provides AWS CLI v1. If you require AWS CLI v2, you MUST use the `aws-cdk-asset-awscli-v2` package instead. Mixing versions or mistakenly assuming v2 can lead to unexpected behavior or missing features.","severity":"gotcha","affected_versions":"All versions of `aws-cdk-asset-awscli-v1`."},{"fix":"Evaluate if a custom layer with only the specific tools or subset of AWS CLI commands you need could be more efficient. Benchmark cold start times in your application.","message":"The AWS CLI layer is substantial in size (around 50MB unzipped). This can contribute to increased Lambda cold start times and potentially push your deployment package size close to Lambda limits. Only include it if truly necessary.","severity":"gotcha","affected_versions":"All versions."},{"fix":"Ensure your Lambda function's runtime (`lambda_.Runtime.PYTHON_X_Y`) matches the `requires_python` specification of the installed `aws-cdk-asset-awscli-v1` version.","message":"This asset package has specific Python runtime requirements (e.g., `~=3.9` as per PyPI). Using a Lambda function with an incompatible Python runtime (e.g., Python 3.7 or Python 3.12 if not supported) may lead to runtime errors when the CLI attempts to execute.","severity":"gotcha","affected_versions":"All versions."}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}