Class AssetCode

java.lang.Object
software.amazon.jsii.JsiiObject
software.amazon.awscdk.services.lambda.Code
software.amazon.awscdk.services.lambda.AssetCode
All Implemented Interfaces:
software.amazon.jsii.JsiiSerializable

@Generated(value="jsii-pacmak/1.84.0 (build 5404dcf)", date="2023-06-19T16:30:40.552Z") @Stability(Stable) public class AssetCode extends Code
Lambda code from a local directory.

Example:

 import path.*;
 import software.amazon.awscdk.services.lambda.*;
 import software.amazon.awscdk.core.App;
 import software.amazon.awscdk.core.Stack;
 import software.amazon.awscdk.services.apigateway.MockIntegration;
 import software.amazon.awscdk.services.apigateway.PassthroughBehavior;
 import software.amazon.awscdk.services.apigateway.RestApi;
 import software.amazon.awscdk.services.apigateway.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 = new App();
 Stack stack = new Stack(app, "TokenAuthorizerInteg");
 Function authorizerFn = Function.Builder.create(stack, "MyAuthorizerFunction")
         .runtime(Runtime.NODEJS_14_X)
         .handler("index.handler")
         .code(AssetCode.fromAsset(join(__dirname, "integ.token-authorizer.handler")))
         .build();
 RestApi restapi = new RestApi(stack, "MyRestApi");
 TokenAuthorizer authorizer = TokenAuthorizer.Builder.create(stack, "MyAuthorizer")
         .handler(authorizerFn)
         .build();
 restapi.root.addMethod("ANY", MockIntegration.Builder.create()
         .integrationResponses(List.of(IntegrationResponse.builder().statusCode("200").build()))
         .passthroughBehavior(PassthroughBehavior.NEVER)
         .requestTemplates(Map.of(
                 "application/json", "{ \"statusCode\": 200 }"))
         .build(), MethodOptions.builder()
         .methodResponses(List.of(MethodResponse.builder().statusCode("200").build()))
         .authorizer(authorizer)
         .build());
 
  • Constructor Details

    • AssetCode

      protected AssetCode(software.amazon.jsii.JsiiObjectRef objRef)
    • AssetCode

      protected AssetCode(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
    • AssetCode

      @Stability(Stable) public AssetCode(@NotNull String path, @Nullable AssetOptions options)
      Parameters:
      path - The path to the asset file or directory. This parameter is required.
      options -
    • AssetCode

      @Stability(Stable) public AssetCode(@NotNull String path)
      Parameters:
      path - The path to the asset file or directory. This parameter is required.
  • Method Details

    • bind

      @Stability(Stable) @NotNull public CodeConfig bind(@NotNull Construct scope)
      Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.

      Specified by:
      bind in class Code
      Parameters:
      scope - This parameter is required.
    • bindToResource

      @Stability(Stable) public void bindToResource(@NotNull CfnResource resource, @Nullable ResourceBindOptions options)
      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.

      Overrides:
      bindToResource in class Code
      Parameters:
      resource - This parameter is required.
      options -
    • bindToResource

      @Stability(Stable) public void bindToResource(@NotNull CfnResource resource)
      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.

      Overrides:
      bindToResource in class Code
      Parameters:
      resource - This parameter is required.
    • getIsInline

      @Stability(Stable) @NotNull public Boolean getIsInline()
      Determines whether this Code is inline code or not.
      Specified by:
      getIsInline in class Code
    • getPath

      @Stability(Stable) @NotNull public String getPath()
      The path to the asset file or directory.