# CfnParametersCode

Lambda code defined using 2 CloudFormation parameters.

Useful when you don't have access to the code of your Lambda from your CDK code, so you can't use Assets,
and you want to deploy the Lambda in a CodePipeline, using CloudFormation Actions -
you can fill the parameters using the `#assign` method.

Extends: Code › CfnParametersCode

- Source: [`aws-lambda/lib/code.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-lambda/lib/code.ts#L612)

## Example

```ts
const lambdaStack = new cdk.Stack(app, 'LambdaStack');
const lambdaCode = lambda.Code.fromCfnParameters();
new lambda.Function(lambdaStack, 'Lambda', {
  code: lambdaCode,
  handler: 'index.handler',
  runtime: lambda.Runtime.NODEJS_LATEST,
});
// other resources that your Lambda needs, added to the lambdaStack...

const pipelineStack = new cdk.Stack(app, 'PipelineStack');
const pipeline = new codepipeline.Pipeline(pipelineStack, 'Pipeline', {
  crossAccountKeys: true,
});

// add the source code repository containing this code to your Pipeline,
// and the source code of the Lambda Function, if they're separate
const cdkSourceOutput = new codepipeline.Artifact();
const cdkSourceAction = new codepipeline_actions.CodeCommitSourceAction({
  repository: new codecommit.Repository(pipelineStack, 'CdkCodeRepo', {
    repositoryName: 'CdkCodeRepo',
  }),
  actionName: 'CdkCode_Source',
  output: cdkSourceOutput,
});
const lambdaSourceOutput = new codepipeline.Artifact();
const lambdaSourceAction = new codepipeline_actions.CodeCommitSourceAction({
  repository: new codecommit.Repository(pipelineStack, 'LambdaCodeRepo', {
    repositoryName: 'LambdaCodeRepo',
  }),
  actionName: 'LambdaCode_Source',
  output: lambdaSourceOutput,
});
pipeline.addStage({
  stageName: 'Source',
  actions: [cdkSourceAction, lambdaSourceAction],
});

// synthesize the Lambda CDK template, using CodeBuild
// the below values are just examples, assuming your CDK code is in TypeScript/JavaScript -
// adjust the build environment and/or commands accordingly
const cdkBuildProject = new codebuild.Project(pipelineStack, 'CdkBuildProject', {
  environment: {
    buildImage: codebuild.LinuxBuildImage.STANDARD_7_0,
  },
  buildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    phases: {
      install: {
        commands: 'npm install',
      },
      build: {
        commands: [
          'npm run build',
          'npm run cdk synth LambdaStack -- -o .',
        ],
      },
    },
    artifacts: {
      files: 'LambdaStack.template.yaml',
    },
  }),
});
const cdkBuildOutput = new codepipeline.Artifact();
const cdkBuildAction = new codepipeline_actions.CodeBuildAction({
  actionName: 'CDK_Build',
  project: cdkBuildProject,
  input: cdkSourceOutput,
  outputs: [cdkBuildOutput],
});

// build your Lambda code, using CodeBuild
// again, this example assumes your Lambda is written in TypeScript/JavaScript -
// make sure to adjust the build environment and/or commands if they don't match your specific situation
const lambdaBuildProject = new codebuild.Project(pipelineStack, 'LambdaBuildProject', {
  environment: {
    buildImage: codebuild.LinuxBuildImage.STANDARD_7_0,
  },
  buildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    phases: {
      install: {
        commands: 'npm install',
      },
      build: {
        commands: 'npm run build',
      },
    },
    artifacts: {
      files: [
        'index.js',
        'node_modules/**/*',
      ],
    },
  }),
});
const lambdaBuildOutput = new codepipeline.Artifact();
const lambdaBuildAction = new codepipeline_actions.CodeBuildAction({
  actionName: 'Lambda_Build',
  project: lambdaBuildProject,
  input: lambdaSourceOutput,
  outputs: [lambdaBuildOutput],
});

pipeline.addStage({
  stageName: 'Build',
  actions: [cdkBuildAction, lambdaBuildAction],
});

// finally, deploy your Lambda Stack
pipeline.addStage({
  stageName: 'Deploy',
  actions: [
    new codepipeline_actions.CloudFormationCreateUpdateStackAction({
      actionName: 'Lambda_CFN_Deploy',
      templatePath: cdkBuildOutput.atPath('LambdaStack.template.yaml'),
      stackName: 'LambdaStackDeployedName',
      adminPermissions: true,
      parameterOverrides: lambdaCode.assign(lambdaBuildOutput.s3Location),
      extraInputs: [
        lambdaBuildOutput,
      ],
    }),
  ],
});
```

## Initializer

```ts
new CfnParametersCode(props?: CfnParametersCodeProps)
```

## Properties

- `bucketNameParam`: string (required)

- `isInline`: boolean (required) — Determines whether this Code is inline code or not.

- `objectKeyParam`: string (required)

## Methods

- `assign(location: Location): { [key: string]: any }` — Create a parameters map from this instance's CloudFormation parameters.

  It returns a map with 2 keys that correspond to the names of the parameters defined in this Lambda code,
  and as values it contains the appropriate expressions pointing at the provided S3 location
  (most likely, obtained from a CodePipeline Artifact by calling the `artifact.s3Location` method).
  The result should be provided to the CloudFormation Action
  that is deploying the Stack that the Lambda with this code is part of,
  in the `parameterOverrides` property.

  **Parameters**

  - `location` — the location of the object in S3 that represents the Lambda code.

- `bind(scope: Construct): CodeConfig` — Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.

- `bindToResource(_resource: CfnResource, _options?: ResourceBindOptions): void` — 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.

- `fromAsset(path: string, options?: AssetOptions): AssetCode` (static) — Loads the function code from a local disk path.

  **Parameters**

  - `path` — Either a directory with the Lambda code bundle or a .zip file.

- `fromAssetImage(directory: string, props?: AssetImageCodeProps): AssetImageCode` (static) — Create an ECR image from the specified asset and bind it as the Lambda code.

  **Parameters**

  - `directory` — the directory from which the asset must be created.
  - `props` — properties to further configure the selected image.

- `fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3Code` (static) — Lambda handler code as an S3 object.

  Note: If `objectVersion` is not defined, the lambda will not be updated automatically if the code in the bucket is updated.
  This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.

  **Parameters**

  - `bucket` — The S3 bucket.
  - `key` — The object key.
  - `objectVersion` — Optional S3 object version.

- `fromBucketV2(bucket: IBucket, key: string, options?: BucketOptions): S3CodeV2` (static) — Lambda handler code as an S3 object.

  Note: If `options.objectVersion` is not defined, the lambda will not be updated automatically if the code in the bucket is updated.
  This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.

  **Parameters**

  - `bucket` — The S3 bucket.
  - `key` — The object key.
  - `options` — Optional parameters for setting the code, current optional parameters to set here are 1.

- `fromCfnParameters(props?: CfnParametersCodeProps): CfnParametersCode // chainable` (static) — Creates a new Lambda source defined using CloudFormation parameters.

  **Parameters**

  - `props` — optional construction properties of `CfnParametersCode`.

  **Returns:** a new instance of `CfnParametersCode`

- `fromCustomCommand(output: string, command: string[], options?: CustomCommandOptions): AssetCode` (static) — Runs a command to build the code asset that will be used.

  **Parameters**

  - `output` — Where the output of the command will be directed, either a directory or a .zip file with the output Lambda code bundle * For example, if you use the command to run a build script (e.g., [ 'node', 'bundle_code.js' ]), and the build script generates a directory `/my/lambda/code` containing code that should be ran in a Lambda function, then output should be set to `/my/lambda/code`.
  - `command` — The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ].
  - `options` — options for the custom command, and other asset options -- but bundling options are not allowed.

- `fromDockerBuild(path: string, options?: DockerBuildAssetOptions): AssetCode` (static) — 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**

  - `path` — The path to the directory containing the Docker file.
  - `options` — Docker build options.

- `fromEcrImage(repository: IRepository, props?: EcrImageCodeProps): EcrImageCode` (static) — Use an existing ECR image as the Lambda code.

  **Parameters**

  - `repository` — the ECR repository that the image is in.
  - `props` — properties to further configure the selected image.

- `fromInline(code: string): InlineCode` (static) — Inline code for Lambda handler.

  **Parameters**

  - `code` — The actual handler code (the resulting zip file cannot exceed 4MB).

  **Returns:** `LambdaInlineCode` with inline code.
