Skip to content

Commit

Permalink
feat: support samconfig.yaml (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlsim0n authored Sep 14, 2023
1 parent baca0db commit 7031efc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Options:


### samp invoke
Invokes a Lambda function or StepFunctions state machine in your CloudFormation stack. If a samconfig.toml file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags.
Invokes a Lambda function or StepFunctions state machine in your CloudFormation stack. If a `samconfig.[toml/yaml]` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags.

You can pass a variety of inputs to the function / state machine:
* A path to a local JSON file
Expand All @@ -101,7 +101,7 @@ Options:
### samp local
This feature is inspired by and works similarly to [SST](https://twitter.com/SST_dev)'s [Live Lambda Development](https://docs.sst.dev/live-lambda-development).

It lets you test your Lambda functions locally with real events from your AWS account. You can step through your code using breakpoints and get sub-second code reloads on changes. If a `samconfig.toml` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags.
It lets you test your Lambda functions locally with real events from your AWS account. You can step through your code using breakpoints and get sub-second code reloads on changes. If a `samconfig.[toml/yaml]` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags.

* NOTE #1: this command temporarily replaces your function code in the cloud with a proxy function that relays events to your local machine over AWS IoT (MQTT). Please only use on development stacks. Never use on production functions! *
* NOTE #2: this command does not fully support Lambda layers. If you use layers to bundle your dependencies, you will have to manually install them locally as well. If you use layers to share custom code between functions, create a symlink from `/opt/nodejs` to the layer folder in your function folder.
Expand Down Expand Up @@ -228,7 +228,7 @@ Options:


### samp console
Launches the AWS console for the selected SAM resource. If a samconfig.toml file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags.
Launches the AWS console for the selected SAM resource. If a `samconfig.[toml/yaml]` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags.

```
Usage: sampat console|c [options]
Expand Down
2 changes: 1 addition & 1 deletion docs/samp-local-dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Prerequisites:
* Function runtime should be `dotnet6`
* Has to be a SAM project. CDK might be supported later.
* It assumes a folder structure where the SAM template is in the root, next to the `samconfig.toml` file
* It assumes a folder structure where the SAM template is in the root, next to the `samconfig.[toml/yaml]` file
* The SAM template can be in either JSON or YAML format and can be called anything with a `json`, `yml`, `yaml` or `.template` extension
* You need to have npm/node installed on your system.

Expand Down
5 changes: 3 additions & 2 deletions src/commands/console/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const parser = require("../../shared/parser");
const fs = require("fs");
const ini = require('ini');
const link2aws = require('link2aws');
const samConfigParser = require('../../shared/samConfigParser');
const open = import('open');
let region;
async function run(cmd) {
if (fs.existsSync("samconfig.toml")) {
const config = ini.parse(fs.readFileSync("samconfig.toml", "utf8"));
if (samConfigParser.configExists()) {
const config = samConfigParser.parse();
const params = config?.default?.deploy?.parameters;
if (!cmd.stackName && params.stack_name) {
console.log("Using stack name from config:", params.stack_name);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/invoke/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { fromSSO } = require("@aws-sdk/credential-provider-sso");
const lambdaInvoker = require("./lambdaInvoker");
const stepFunctionsInvoker = require("./stepFunctionsInvoker");
const inputUtil = require('../../shared/inputUtil');
const samConfigParser = require('../../shared/samConfigParser');
const parser = require("../../shared/parser");
const fs = require("fs");
const ini = require('ini');
Expand Down Expand Up @@ -42,8 +43,8 @@ async function run(cmd) {
console.log("Using default profile. Override by setting --profile <your profile>");
}
}
else if (fs.existsSync("samconfig.toml")) {
const config = ini.parse(fs.readFileSync("samconfig.toml", "utf8"));
else if (samConfigParser.configExists()) {
const config = samConfigParser.parse();
const params = { ...config?.default?.deploy?.parameters, ...(config?.default?.global?.parameters || {}) };
if (!cmd.stackName && params.stack_name) {
console.log("Using stack name from config:", params.stack_name);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/local/lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (fs.existsSync(`${homedir()}/.lambda-debug/uuid`)) {
}

let targetConfig = {};
if (fs.existsSync(`samconfig.toml`)) {
if (samConfigParser.configExists()) {
targetConfig = samConfigParser.parse();
}
targetConfig = {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/local/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ function validate(env) {
}
}

if (!fs.existsSync("samconfig.toml") && !fs.existsSync("cdk.json")) {
console.log("No samconfig.toml found. Please make sure you have deployed your functions before running this command. You can deploy your functions by running 'sam deploy --guided'");
//return false;
if (!samConfigParser.configExists() && !fs.existsSync("cdk.json")) {
console.log("No samconfig found. Please make sure you have deployed your functions before running this command. You can deploy your functions by running 'sam deploy --guided'");
return false;
}

if (fs.existsSync("cdk.json") && !fs.existsSync("cdk.out")) {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/local/runtime-env-finder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const { parse, findSAMTemplateFile } = require('../../shared/parser');
const samConfigParser = require('../../shared/samConfigParser');

function determineRuntime() {
const templateFile = findSAMTemplateFile(process.cwd());
Expand Down Expand Up @@ -41,7 +42,7 @@ function determineRuntime() {
if (csprojFiles.length > 0) return { iac: "sam", functionLanguage: "dotnet" };;

if (fs.existsSync('nuget.config')) return { iac: "sam", functionLanguage: "dotnet" };;
if (fs.existsSync('samconfig.toml')) return { iac: "sam", functionLanguage: "js", isNodeJS: true };;
if (samConfigParser.configExists()) return { iac: "sam", functionLanguage: "js", isNodeJS: true };;
}

module.exports = {
Expand Down
38 changes: 32 additions & 6 deletions src/shared/samConfigParser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const toml = require('toml');
const fs = require('fs');
const yaml = require('yaml');
const path = require('path');

function parse() {
if (!fs.existsSync(`samconfig.toml`)) {
const foundSamconfigFile = findConfigFile();
if (foundSamconfigFile === null) {
return {};
}

const configEnv = 'default';
let config;
try {
config = toml.parse(fs.readFileSync(`samconfig.toml`, 'utf-8'));
} catch (e) {
config = yaml.parse(fs.readFileSync(`samconfig.yaml`, 'utf-8'));
const type = path.extname(foundSamconfigFile);

switch(type) {
case '.toml':
config = toml.parse(fs.readFileSync(foundSamconfigFile, 'utf-8'));
break;
case '.yaml':
config = yaml.parse(fs.readFileSync(foundSamconfigFile, 'utf-8'));
break;
}

const envConfig = config[configEnv].deploy.parameters;
envConfig.configEnv = process.env.configEnv || 'default';
envConfig.stack_name = envConfig.stack_name || config[configEnv].global.parameters.stack_name
Expand All @@ -20,6 +30,22 @@ function parse() {
return envConfig;
}

function findConfigFile() {
const defaultSamconfigFiles = ['samconfig.toml', 'samconfig.yaml'];
for (const file of defaultSamconfigFiles) {
if (fs.existsSync(file)){
return file;
}
}
return null;
}

function configExists() {
return findConfigFile() ? true : false;
}

module.exports = {
parse
parse,
configExists,
findConfigFile
}

0 comments on commit 7031efc

Please sign in to comment.