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

Lambda Specific Configuration does not work with CDK #1998

Open
hnishi opened this issue Oct 25, 2022 · 3 comments
Open

Lambda Specific Configuration does not work with CDK #1998

hnishi opened this issue Oct 25, 2022 · 3 comments

Comments

@hnishi
Copy link
Contributor

hnishi commented Oct 25, 2022

I am using Chalice with CDK to create a REST API and a scheduled lambda function (for a batch job).

I want to specify the configuration for the scheduled lambda function.

@app.schedule(Cron(0, 16, "L", "*", "?", "*"))
def foo(event, context):
    print("hello world")

In the CDK file, the following configuration is used.

class ChaliceApp(cdk.Stack):
    def __init__(self, scope, id, **kwargs):
        super().__init__(scope, id, **kwargs)
        self.dynamodb_table = self._create_ddb_table()
        self.chalice = Chalice(
            self,
            "ChaliceApp",
            source_dir=RUNTIME_SOURCE_DIR,
            stage_config={
                "environment_variables": {
                    "APP_TABLE_NAME": self.dynamodb_table.table_name
                },
                "lambda_functions": {
                    "foo": {
                        "lambda_timeout": 600,
                        "lambda_memory_size": 256,
                        "environment_variables": {"HELLO": "world"},
                    }
                },
            },
        )
        self.dynamodb_table.grant_read_write_data(self.chalice.get_role("DefaultRole"))

However, after deploying the application with cdk deploy, the scheduled lambda function is created with the default timeout (60 sec) and memory size (128 MB).

I think the Lambda Specific Configuration does not work with CDK.

@hnishi
Copy link
Contributor Author

hnishi commented Oct 26, 2022

The current code only reads _config_from_disk in config.py.

chalice/chalice/config.py

Lines 197 to 209 in f64bd82

if varies_per_function:
# search order:
# config['stages']['lambda_functions']
# config['stages']
# config['lambda_functions']
search_dicts.insert(
0, self._config_from_disk.get('stages', {}).get(
self.chalice_stage, {}).get('lambda_functions', {}).get(
self.function_name, {}))
search_dicts.append(
self._config_from_disk.get('lambda_functions', {}).get(
self.function_name, {}))
search_dicts.extend([self._config_from_disk, self._default_params])

I created a PR.

#2000

I found a workaround to set up the lambda specific configuration with CDK and the current code.

Add the following configuration in ../runtime/.chalice/config.json.

In the CDK configuration step, the stage name will be the app_name (e.g. cdkdemo in this example). Thus add the configuration for the stage name.

{
  "version": "2.0",
  "app_name": "cdkdemo",
  "stages": {
      "cdkdemo": {
            "lambda_functions": {
                "foo": {
                    "lambda_timeout": 200,
                    "lambda_memory_size": 256,
                    "environment_variables": {"HELLO": "world2"}
                }
            }
    },
    "dev": {
      "api_gateway_stage": "api",
      "lambda_functions": {
        "api_handler": {
          "environment_variables": {
            "APP_TABLE_NAME": "xxx"
          }
        },
        "foo": {
          "environment_variables": {
            "APP_TABLE_NAME": "yyy"
          },
          "lambda_timeout": 900,
          "lambda_memory_size": 256
        }
      }
    }
  }
}

@MacHu-GWU
Copy link

MacHu-GWU commented Jul 3, 2023

I feel like the chalice + cdk deployment is super inmature. lots of things are not configurable and the old .chalice/config.json is not working and also not mentioned in the document. Don't get me wrong, I am a big fan of chalice.

I am working a a team having around 250+ lambda function in production and 10+ different internal api. We have tried pure CDK, Chalice + CDK, and eventually we choose CDK for iam role, and infra, then explicitly use those values in Chalice / config.json and deploy in the "classic chalice" way, and use S3 to store deployed.json

@mateuspadua
Copy link

I think this answer can help:
#1846 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants