AssetCode — AWS Cloud Development Kit 1.204.0 documentation (original) (raw)

AWS Cloud Development Kit

class aws_cdk.aws_lambda.AssetCode(path, *, readers=None, source_hash=None, exclude=None, follow=None, ignore_mode=None, follow_symlinks=None, asset_hash=None, asset_hash_type=None, bundling=None)

Bases: Code

Lambda code from a local directory.

ExampleMetadata:

lit=test/authorizers/integ.token-authorizer.lit.ts infused

Example:

from aws_cdk.aws_apigateway import IntegrationResponse, MethodResponse import path as path import aws_cdk.aws_lambda as lambda_ from aws_cdk.core import App, Stack from aws_cdk.aws_apigateway import MockIntegration, PassthroughBehavior, RestApi, TokenAuthorizer

Stack verification steps:

* curl -s -o /dev/null -w "%{http_code}" <url> should return 401

* curl -s -o /dev/null -w "%{http_code}" -H 'Authorization: deny' <url> should return 403

* curl -s -o /dev/null -w "%{http_code}" -H 'Authorization: allow' <url> should return 200

app = App() stack = Stack(app, "TokenAuthorizerInteg")

authorizer_fn = lambda_.Function(stack, "MyAuthorizerFunction", runtime=lambda_.Runtime.NODEJS_14_X, handler="index.handler", code=lambda_.AssetCode.from_asset(path.join(__dirname, "integ.token-authorizer.handler")) )

restapi = RestApi(stack, "MyRestApi")

authorizer = TokenAuthorizer(stack, "MyAuthorizer", handler=authorizer_fn )

restapi.root.add_method("ANY", MockIntegration( integration_responses=[IntegrationResponse(status_code="200") ], passthrough_behavior=PassthroughBehavior.NEVER, request_templates={ "application/json": "{ "statusCode": 200 }" } ), method_responses=[MethodResponse(status_code="200") ], authorizer=authorizer )

Parameters:

Methods

bind(scope)

Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.

Parameters:

scope (Construct)

Return type:

CodeConfig

bind_to_resource(resource, *, resource_property=None)

Called after the CFN function resource has been created to allow the code class to bind to it.

Specifically it’s required to allow assets to add metadata for tooling like SAM CLI to be able to find their origins.

Parameters:

Return type:

None

Attributes

is_inline

Determines whether this Code is inline code or not.

path

The path to the asset file or directory.

Static Methods

classmethod asset(path)

(deprecated) DEPRECATED.

Parameters:

path (str)

Deprecated:

use fromAsset

Stability:

deprecated

Return type:

AssetCode

classmethod bucket(bucket, key, object_version=None)

(deprecated) DEPRECATED.

Parameters:

Deprecated:

use fromBucket

Stability:

deprecated

Return type:

S3Code

classmethod cfn_parameters(*, bucket_name_param=None, object_key_param=None)

(deprecated) DEPRECATED.

Parameters:

Deprecated:

use fromCfnParameters

Stability:

deprecated

Return type:

CfnParametersCode

classmethod from_asset(path, *, readers=None, source_hash=None, exclude=None, follow=None, ignore_mode=None, follow_symlinks=None, asset_hash=None, asset_hash_type=None, bundling=None)

Loads the function code from a local disk path.

Parameters:

Return type:

AssetCode

classmethod from_asset_image(directory, *, cmd=None, entrypoint=None, working_directory=None, build_args=None, file=None, invalidation=None, network_mode=None, platform=None, repository_name=None, target=None, extra_hash=None, exclude=None, follow=None, ignore_mode=None, follow_symlinks=None)

Create an ECR image from the specified asset and bind it as the Lambda code.

Parameters:

Return type:

AssetImageCode

classmethod from_bucket(bucket, key, object_version=None)

Lambda handler code as an S3 object.

Parameters:

Return type:

S3Code

classmethod from_cfn_parameters(*, bucket_name_param=None, object_key_param=None)

Creates a new Lambda source defined using CloudFormation parameters.

Parameters:

Return type:

CfnParametersCode

Returns:

a new instance of CfnParametersCode

classmethod from_docker_build(path, *, image_path=None, output_path=None, build_args=None, file=None, platform=None)

Loads the function code from an asset created by a Docker build.

By default, the asset is expected to be located at /asset in the image.

Parameters:

Return type:

AssetCode

classmethod from_ecr_image(repository, *, cmd=None, entrypoint=None, tag=None, tag_or_digest=None, working_directory=None)

Use an existing ECR image as the Lambda code.

Parameters:

Return type:

EcrImageCode

classmethod from_inline(code)

Inline code for Lambda handler.

Parameters:

code (str) – The actual handler code (limited to 4KiB).

Return type:

InlineCode

Returns:

LambdaInlineCode with inline code.

classmethod inline(code)

(deprecated) DEPRECATED.

Parameters:

code (str)

Deprecated:

use fromInline

Stability:

deprecated

Return type:

InlineCode