Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional parameter for function name #471

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions guard-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,50 @@ The Lambda version of the tool is a lightweight wrapper around the core [cfn-gua

### Building and deploying

#### Guided

1. Make sure docker is running
2. Navigate to `guard-lambda` directory and run `sam build --use-container` to build the code for the Lambda function
3. Run `sam deploy --guided` and complete the interactive workflow. This workflow will create a CloudFormation changeset and deploy it
4. Once it succeeds, the name of the function will be shown in the `CloudFormationGuardLambdaFunctionName` output
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are asking user to pass a parameter for name this line will change, correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a samconfig.toml is not being used, you are correct. sam deploy will need this added. I will update the PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood - no, I don't think this will need to change. The output does not change by having a stack parameter. Also, if following the guided approach, it should be managed through the interactive process. The parameter is not required, so already existing consumers will not need to add it.

5. For subsequent updates, build the code again (step 2) and run `sam deploy` (without `--guided`)

#### CI/CD

This approach does not require user input and can be used in CI/CD pipelines:

1. Make sure docker is running
2. Navigate to `guard-lambda` directory and run `sam build --use-container` to build the code for the Lambda function
3. Run `sam package --s3-bucket <your-bucket-name>` this will create a `.zip` package with the code and dependencies and upload to S3
4. Run `sam deploy --s3-bucket <your-bucket-name> --stack-name <your-stack-name` this will deploy the application to CloudFormation
5. Once it succeeds, the name of the function will be shown in the `CloudFormationGuardLambdaFunctionName` output. To retrieve this programmatically, run ` sam list stack-outputs --output json`. If `jq` is available, you can retrieve the value by running `sam list stack-outputs --output json | jq -r .[0].OutputValue`

See [here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html) for all `sam package` options
see [here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html) for all sam deploy options

Alternatively, you can build a `somconfig.toml` in your directory and specify any and all CLI options in [toml](https://toml.io/en/) format. AWS Documentation [here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html).

#### Specifying Stack Parameters

To specify a stack parameter, add `--parameter-overrides` option to the `sam deploy` command. For example, to specify the `FunctionName`, run the following command:

```bash
sam deploy \
--s3-bucket <your-bucket-name> \
--stack-name <your-stack-name> \
--parameter-overrides FunctionName=MyCfnGuardLambda
```

or in your `samconfig.toml`:

```toml
[default.global.parameters]
s3_bucket = "<your-bucket-name>"
stack_name = "<your-stack-name>"
parameter_overrides = "FunctionName=MyCfnGuardLambda"
```

Note: multiple parameters are separated by spaces: `Param1=Value1 Param2=Value2`

## Calling the AWS Lambda Function

Expand Down
6 changes: 6 additions & 0 deletions guard-lambda/template.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
Transform: AWS::Serverless-2016-10-31

Parameters:
FunctionName:
Type: String
Description: The name of the function

Resources:
CloudFormationGuardLambda:
Type: AWS::Serverless::Function
Properties:
Runtime: provided.al2
Handler: guard.handler
FunctionName: !Ref FunctionName
# We need to point to the parent directory, so we can use ../guard/*
CodeUri: ..
Environment:
Expand Down