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

Fixed issues with incorrect work directory and missing environment variables. #17

Open
wants to merge 204 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
204 commits
Select commit Hold shift + click to select a range
2a81f0c
Unzip the file into the v/ar/task/ folder, so it can find it's files.
rlyle Nov 23, 2020
b6f0899
1.0.8
rlyle Nov 23, 2020
009e532
Updating package name.
rlyle Nov 23, 2020
89d7502
Revert "Unzip the file into the v/ar/task/ folder, so it can find it'…
rlyle Nov 23, 2020
c8d7f84
Don't prefix the addtional commands with RUN, so I can use the WORKDI…
rlyle Nov 23, 2020
e274468
1.0.9
rlyle Nov 23, 2020
ae1edbf
Automatically set the work directory to where the package is extracted.
rlyle Nov 23, 2020
474d618
1.0.10
rlyle Nov 23, 2020
8f984bb
Pass in the function name and XRAY trace ID into the batch job.
rlyle Nov 25, 2020
1ff01b6
1.0.11
rlyle Nov 25, 2020
fd0aee1
Adding addtional enviroment variables to pass from the lambda into th…
rlyle Nov 25, 2020
9653d0f
1.0.12
rlyle Nov 25, 2020
2d0694e
Restoring the RUN command, was not needed after all.
rlyle Nov 25, 2020
02d4d1d
Removing the log stream enviornment variables, not seeing logs curren…
rlyle Nov 25, 2020
43f69e1
1.0.13
rlyle Nov 25, 2020
1bf62b6
Removing other enironment variables except for the ones we really need.
rlyle Nov 25, 2020
72d25aa
1.0.14
rlyle Nov 25, 2020
4688297
Allow the lambda schedule function to use XRAY.
rlyle Nov 26, 2020
80a4ea4
1.0.15
rlyle Nov 26, 2020
cebcb26
Fixed typo.
rlyle Nov 26, 2020
ec508ba
1.0.16
rlyle Nov 26, 2020
9de1645
Adding support for XRAY.
rlyle Nov 26, 2020
e439919
1.0.17
rlyle Nov 26, 2020
8d97fdb
Removing the _X_AMAZ_TRACE_ID, to see if XRAY work inside the AWS bat…
rlyle Nov 27, 2020
ea321db
1.0.18
rlyle Nov 27, 2020
a535146
Restoring the _X_AMZN_TRACE_ID environment variable.
rlyle Nov 27, 2020
53c90a4
1.0.19
rlyle Nov 27, 2020
9a7df20
Adding AWS_REQUEST_ID to the environment variables to see if this works.
rlyle Dec 1, 2020
4905942
1.0.20
rlyle Dec 1, 2020
988bbaf
Trying to fix the error: The provided execution role does not have pe…
rlyle Dec 3, 2020
4a1e4b1
1.0.21
rlyle Dec 3, 2020
d6bff61
IOA-15971 | Seperating the EC2 permissions from the XRAY ones.
rlyle Oct 18, 2021
c6c2453
1.0.22
rlyle Oct 18, 2021
1e1ca55
Updating to nodejs12.x
rlyle Nov 3, 2021
e8089bf
1.0.23
rlyle Nov 3, 2021
6b67b95
Explicity add the access keys to the batch env.
rlyle Nov 4, 2021
45b1403
1.0.24
rlyle Nov 4, 2021
d047540
Removing the XRAY trace ID, we are pushed over the limit for containe…
rlyle Jan 27, 2022
98b396b
1.0.25
rlyle Jan 27, 2022
43f9f5b
Removing the headers from the event before we submit a job, we are ex…
rlyle Jan 27, 2022
7adf30a
1.0.26
rlyle Jan 27, 2022
a62ea1b
HOTFIX: Return an access-control-allow-origin header.
rlyle Jan 27, 2022
ee576db
1.0.27
rlyle Jan 27, 2022
515f87e
Don't save an absolute path into the template file, make's it impossi…
rlyle Feb 17, 2022
cf523b7
1.0.28
rlyle Feb 17, 2022
0f1b032
Undo my last change, log out the config so we can see the contents of…
rlyle Feb 17, 2022
5fe2bb8
1.0.29
rlyle Feb 17, 2022
6914bc6
Hook the after:aws.common:moveArtifactsToPackage:move to see what dat…
rlyle Feb 17, 2022
372459a
1.0.30
rlyle Feb 17, 2022
18fedde
Reverting last change.
rlyle Feb 17, 2022
c0e4b9a
1.0.31
rlyle Feb 17, 2022
7c71b09
Don't use latest for the image names, as this doesn't allow us to bui…
rlyle Feb 25, 2022
759c60d
1.0.32
rlyle Feb 25, 2022
cf688ad
We need to initialize a variable, so the calls to getDockerImageName(…
rlyle Feb 25, 2022
b0e279d
1.0.33
rlyle Feb 25, 2022
cec2d8a
Updated the life-cycle policy on the ECR to just delete images over 9…
rlyle Mar 4, 2022
4b8112e
1.0.34
rlyle Mar 4, 2022
5c7c119
Updated variable name.
rlyle Mar 4, 2022
c4a841c
Fixing text.
rlyle Mar 4, 2022
8d27986
1.0.35
rlyle Mar 4, 2022
bfaf97b
HOTFIX: I think we need to double escape the life cycle policy text.
rlyle Mar 4, 2022
b88508a
1.0.36
rlyle Mar 4, 2022
f9784c0
HOTFIX: Adding a log to see what the ecrTemplate looks like before we…
rlyle Mar 4, 2022
bcb5481
1.0.37
rlyle Mar 4, 2022
5687cb2
HOTFIX: This should have worked [skip test]
rlyle Mar 4, 2022
3187985
1.0.38
rlyle Mar 4, 2022
38a6230
HOTFIX: Removing the outer quotes, not needed. [skip test]
rlyle Mar 4, 2022
5d1049f
1.0.39
rlyle Mar 4, 2022
dacfbcc
HOTFIX: Removing console.log now that it's working. [skip test]
rlyle Mar 4, 2022
e906130
1.0.40
rlyle Mar 4, 2022
36957bf
HOTFIX: output the content of stdout and stderr when the docker comma…
rlyle Jun 16, 2022
81b5473
1.0.41
rlyle Jun 16, 2022
407f7c1
HOTFIX: Removed the status check when running docker.
rlyle Jun 16, 2022
eac1b60
1.0.42
rlyle Jun 16, 2022
3e8fd68
HOTIFIX: Log the errors but don't throw when the status code is not 0.
rlyle Jun 16, 2022
629d4f6
1.0.43
rlyle Jun 16, 2022
2c5ad49
HOTFIX: Adding log when a docker command is ran. Make sure DOCKER_TAG…
rlyle Jun 16, 2022
6a75bc5
1.0.44
rlyle Jun 16, 2022
46409ae
HOTFIX: Not seeing the console.log, so changed to console.error. [ski…
rlyle Jun 16, 2022
f9175a8
1.0.45
rlyle Jun 16, 2022
f8627d2
HOTIFIX: INclude the DOCKER_TAG when we call getRespositoryURL(). [sk…
rlyle Jun 16, 2022
bdafb57
1.0.46
rlyle Jun 16, 2022
72414c8
HOTFIX: need to use the --all-tags option with docker push now. [skip…
rlyle Jun 16, 2022
e243a34
1.0.47
rlyle Jun 16, 2022
f5d04a5
HOTFIX: Removing some console.errors I added to debug. [skip test]
rlyle Jun 16, 2022
907d08a
1.0.48
rlyle Jun 16, 2022
be63ac1
Always set the NODE_OPTIONS variable.
rlyle Aug 4, 2022
3eb0746
1.0.49
rlyle Aug 4, 2022
315ab83
Trying to add support for nodejs16.x
rlyle Jun 7, 2023
eca9674
1.0.50
rlyle Jun 7, 2023
778b531
IOA-22224 | Lets try running batch under node 12, it should be fine. …
rlyle Jun 7, 2023
af86038
1.0.51
rlyle Jun 7, 2023
356c3cf
Revert "IOA-22224 | Lets try running batch under node 12, it should b…
rlyle Jun 7, 2023
a90246c
IOA-22224 | Trying to get the nodejs 16 in the docker image working.
rlyle Jun 7, 2023
298dc9a
1.0.52
rlyle Jun 7, 2023
bdd8937
IOA-22245 | Getting node 16 setup in our docker images.
rlyle Jun 7, 2023
50cfcf7
1.0.53
rlyle Jun 7, 2023
c629c60
IOA-22245 | sudo command not found :(
rlyle Jun 7, 2023
46f7065
1.0.54
rlyle Jun 7, 2023
5a59f97
IOA-22245 | Getting node 16 setup in our docker images. [skip test] […
rlyle Jun 7, 2023
85d88a8
1.0.55
rlyle Jun 7, 2023
cbfec23
IOA-22245 | Getting node 16 setup in our docker images. [skip test] […
rlyle Jun 7, 2023
9fba7b9
1.0.56
rlyle Jun 7, 2023
e101232
IOA-22245 | Getting node 16 setup in our docker images. [skip test] […
rlyle Jun 7, 2023
d73f75f
1.0.57
rlyle Jun 7, 2023
400d54a
IOA-22245 | Getting node 16 setup in our docker images. [skip test] […
rlyle Jun 7, 2023
cc5a001
1.0.58
rlyle Jun 7, 2023
d8eac90
IOA-22245 | Getting node 16 setup in our docker images. [skip test] […
rlyle Jun 7, 2023
5c5b87f
1.0.59
rlyle Jun 7, 2023
1542deb
IOA-22245 | Getting node 16 setup in our docker images. [skip test] […
rlyle Jun 7, 2023
7fb0feb
1.0.60
rlyle Jun 7, 2023
3d072fa
IOA-22245 | Trying dnf install yum. [skip test] [skip subs]
rlyle Jun 7, 2023
e7d99dc
1.0.61
rlyle Jun 7, 2023
85f0936
IOA-22245 | try using the full path to yum. [skip test] [skip subs]
rlyle Jun 7, 2023
ba151d7
1.0.62
rlyle Jun 7, 2023
77c2ea6
IOA-22245 | Lets attempt to use the AWS lambda image. [skip test] [sk…
rlyle Jun 7, 2023
5f42818
1.0.63
rlyle Jun 7, 2023
9076f29
IOA-22245 | Added the CMD [ "app.handler" ] to the dockerfile. [skip …
rlyle Jun 7, 2023
ac54857
1.0.64
rlyle Jun 7, 2023
b21dfe6
IOA-22245 | Seems we need to clear the entry point, so the script can…
rlyle Jun 7, 2023
3faaaeb
1.0.65
rlyle Jun 7, 2023
75fb2c9
IOA-22245 | The ENTRYPOINT may need to be node. [skip test] [skip subs]
rlyle Jun 7, 2023
ea243cf
1.0.66
rlyle Jun 7, 2023
82d4a81
IOA-22245 | Pass one argument into the container, passing two causes …
rlyle Jun 7, 2023
eb135c4
1.0.67
rlyle Jun 7, 2023
075dc18
IOA-22245 | Updating the batch lambda to node 16 as well. [skip test…
rlyle Jun 7, 2023
4b47361
IOA-22245 | I think the WORKDIR command is messing up the bootstrap. …
rlyle Jun 7, 2023
63584a6
1.0.68
rlyle Jun 7, 2023
53bfdb4
IOA-22245 | Pass the handler name without the prefixed directory. [sk…
rlyle Jun 7, 2023
658c102
1.0.69
rlyle Jun 7, 2023
0330932
IOA-22245 | Extract the service directly into the /var/task/ director…
rlyle Jun 7, 2023
22cd8ee
1.0.70
rlyle Jun 7, 2023
4174cde
IOA-22245 | Went back to the other image, going to copy curl/yum from…
rlyle Jun 7, 2023
356d8b8
1.0.71
rlyle Jun 7, 2023
bfd7a7c
IOA-22245 | We need the libcurl.so.4 file to run curl. [skip test] […
rlyle Jun 7, 2023
5143530
1.0.72
rlyle Jun 7, 2023
3608f74
IOA-22245 | We need the libcurl.* libs to run curl. [skip test] [ski…
rlyle Jun 7, 2023
e727f80
1.0.73
rlyle Jun 7, 2023
0d515db
IOA-22245 | Copy the entire lib64 and lib folders. [skip test] [skip …
rlyle Jun 7, 2023
e87240b
1.0.74
rlyle Jun 7, 2023
952c0b0
IOA-22245 | Copy yum as well. [skip test] [skip subs]
rlyle Jun 7, 2023
675986a
1.0.75
rlyle Jun 7, 2023
1966900
1.0.76
rlyle Jun 7, 2023
4586463
IOA-22245 | Ok, hopfully this fixes the issue. [skip test] [skip subs]
rlyle Jun 8, 2023
3620597
1.0.77
rlyle Jun 8, 2023
1c06744
IOA-22245 | Clean up some files and set the NODE_PATH. [skip test] […
rlyle Jun 8, 2023
eb56337
1.0.78
rlyle Jun 8, 2023
6a359eb
IOA-22245 | We need tar to extract the binaries. [skip test] [skip s…
rlyle Jun 8, 2023
7ee6343
1.0.79
rlyle Jun 8, 2023
c7c57bf
IOA-22245 | tar needs gzip [skip test] [skip subs]
rlyle Jun 8, 2023
54c21dc
1.0.80
rlyle Jun 8, 2023
4213d67
IOA-22245 | We have the rm -rf /tmp/* at the end, so we can remove th…
rlyle Jun 8, 2023
cbe4e35
1.0.81
rlyle Jun 8, 2023
4bd31bd
IOA-22245 | Switch to the root user when doing the file copies. [skip…
rlyle Jun 8, 2023
9f44c2c
1.0.82
rlyle Jun 8, 2023
ec5072d
IOA-22245 | Need to add /var/task/node_modules to NODE_PATH. [skip te…
rlyle Jun 8, 2023
df3b361
1.0.83
rlyle Jun 8, 2023
795d725
IOA-22245 | Cleaned up the docker build process, do most of the thing…
rlyle Jun 8, 2023
ded006c
1.0.84
rlyle Jun 8, 2023
4e2aa73
IOA-22245 | Need to add /var/runtime/node_modules so we can find the …
rlyle Jun 8, 2023
9b6ca85
1.0.85
rlyle Jun 8, 2023
9ba352e
IOA-22245 | Adding node 16 to our public web contents, so we can pull…
rlyle Jun 8, 2023
ac94585
1.1.0
rlyle Jun 8, 2023
e367268
IOA-22245 | Fixed typo in the URL. [skip test][skip subs]
rlyle Jun 8, 2023
40f5270
1.1.1
rlyle Jun 8, 2023
eb8fcad
HOTFIX: additonalRunCommands should be on the actual image our lambda…
rlyle Jul 21, 2023
a9b378c
1.1.1
rlyle Jul 21, 2023
85fa2a7
1.1.2
rlyle Jul 21, 2023
3c16383
Merge branch 'master' of https://github.com/rlyle/serverless-aws-batch
rlyle Jul 21, 2023
1d95309
HOTFIX: run the additional command before we clean up.
rlyle Jul 21, 2023
1aa9321
1.1.3
rlyle Jul 21, 2023
e7113d2
Add addtionalBuildCommands so we can run any command on the build ima…
rlyle Jul 21, 2023
24f1770
1.1.4
rlyle Jul 21, 2023
b36a88a
Removing the hard-coded commands to install node 16.x, instead doing …
rlyle Jul 21, 2023
fee974a
1.1.5
rlyle Jul 21, 2023
97f7503
IOA-23313 | Updating to pull from our private respository.
rlyle Sep 27, 2023
4def15d
1.1.6
rlyle Sep 27, 2023
c4613ed
HOTFIX: Removing the 90 day Lifecycle policy.
rlyle Oct 2, 2023
a034a54
1.1.7
rlyle Oct 2, 2023
feee2d0
Ran into a problem with cloud formation failing when it's generating …
rlyle Dec 20, 2023
6f480b0
1.1.8
rlyle Dec 20, 2023
13569d5
IOA-23781 | Add the DependsOn to the IAM execution row, since the rol…
rlyle Dec 20, 2023
466be19
1.1.9
rlyle Dec 20, 2023
7121f10
IOA-23781 | Need to apply the DependsOn in the right place for the ex…
rlyle Dec 20, 2023
342e13a
1.1.10
rlyle Dec 20, 2023
c566bac
1.1.11
rlyle Dec 20, 2023
ca61097
Allow the build image name to be pulled from the serverless.yaml file…
rlyle Feb 26, 2024
7079385
Updating version to 1.1.10
rlyle Feb 26, 2024
f888930
Merged other changes made on my other machine.
rlyle Feb 26, 2024
428ede5
Updating version to 1.1.12
rlyle Feb 26, 2024
9c0c5e0
HOTFIX: Added batchImage support as well.
rlyle Feb 26, 2024
8a584b0
Updating to version 1.1.13
rlyle Feb 26, 2024
dd4c13d
Use the runtime from the provider section of the config by default in…
rlyle Jul 5, 2024
9feb32f
1.1.14
rlyle Jul 5, 2024
03f546e
Default to node v18, use a variable from the custom config section in…
Jul 5, 2024
8f617e0
1.1.15
Jul 5, 2024
2dbe4b6
Updated to use AWS sdk v3.
rlyle Jul 8, 2024
0bb0e4f
1.1.16
rlyle Jul 8, 2024
3808ae8
MRF-162 | Adding support for the serverless-log-forwarding plugin, so…
Oct 3, 2024
685d748
1.1.17
Oct 3, 2024
633686b
MRF-162 | We are going to need a different log group for batch jobs i…
Oct 3, 2024
d0566dd
1.1.18
Oct 3, 2024
93d5ee9
MRF-162 | Fixed typo.
Oct 3, 2024
a644b41
1.1.19
Oct 3, 2024
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
22 changes: 22 additions & 0 deletions lib/batchenvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ function generateLambdaScheduleExecutionRole() {
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"xray:PutTraceSegments",
Copy link
Author

Choose a reason for hiding this comment

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

Allow XRAY to work from the lambda.

"xray:PutTelemetryRecords"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
Copy link
Author

Choose a reason for hiding this comment

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

Fixes the error creating the lambda for the first time.

"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface",
"ec2:DeleteNetworkInterface"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
Expand Down
35 changes: 31 additions & 4 deletions lib/batchtask.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function getBatchJobExecutionRoleLogicalId(functionName) {
: logicalId;
}

let dependsOnFunction = undefined;

/**
* Transforms a function object into a "JobDefinition" object that can be used to run this function inside a Batch task
*/
Expand Down Expand Up @@ -54,14 +56,26 @@ function compileBatchTask(functionName) {
updateRetryStrategy.bind(this)(newFunction, functionObject);
updateTimeout.bind(this)(newFunction, functionObject);

const logicalId = getJobDefinitionLogicalId(functionName);

// generate role for a batchjob execution based on custom iam statements
const newBatchJobExecutionRoleObject = generateBatchJobExecutionRole.bind(this)(functionName, functionObject)

// FIX: CloudFormation is blowing up trying to create all the JobDefinitions in parallel,
// so to fix we make each JobDefinition depend on the prior one being created to solve this issue.
if ( dependsOnFunction !== undefined ) {
for(let k in newBatchJobExecutionRoleObject) {
newBatchJobExecutionRoleObject[k]["DependsOn"] = dependsOnFunction;
}
newFunction["DependsOn"] = dependsOnFunction;
}
dependsOnFunction = logicalId;

// Add it to our compiled cloud formation templates
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
{
[getJobDefinitionLogicalId(functionName)]: newFunction
[logicalId]: newFunction
},
newBatchJobExecutionRoleObject
);
Expand All @@ -77,7 +91,7 @@ function compileBatchTask(functionName) {
name: functionObject.name,
memorySize: 128,
timeout: 6,
runtime: 'nodejs10.x',
runtime: _.get(this.serverless.service,"custom.runtime", "nodejs18.x" ),
package: {
artifact: `${path.join(this.serverless.config.servicePath, '.serverless', functionName)}.zip`
},
Expand Down Expand Up @@ -124,7 +138,7 @@ function updateContainerProperties(newFunction, functionObject, functionName) {
throw new this.serverless.classes.Error(`Could not find handler for batch task ${functionObject.name}`);
}
newFunction.Properties.ContainerProperties.Command = [
`${this.serverless.service.service}/${functionObject.handler}`,
`${functionObject.handler}`,
"Ref::event"
]
}
Expand All @@ -148,7 +162,9 @@ function updateContainerProperties(newFunction, functionObject, functionName) {
{
'AWS_LAMBDA_FUNCTION_TIMEOUT': getTimeout(functionObject),
'AWS_LAMBDA_FUNCTION_MEMORY_SIZE': getMemorySize(functionObject),
'AWS_REGION': this.serverless.service.provider.region || 'us-east-1'
'AWS_REGION': this.serverless.service.provider.region || 'us-east-1',
'AWS_SECRET_ACCESS_KEY': process.env.AWS_SECRET_ACCESS_KEY,
'AWS_ACCESS_KEY_ID': process.env.AWS_ACCESS_KEY_ID
},
this.serverless.service.provider.environment,
functionObject.environment,
Expand Down Expand Up @@ -190,6 +206,17 @@ function updateContainerProperties(newFunction, functionObject, functionName) {
});
}
);

const logGroupName = _.get(this.serverless.service,"custom.awsBatch.logGroupName");
if ( logGroupName ) {
newFunction.Properties.ContainerProperties.LogConfiguration = {
LogDriver: 'awslogs',
Options: {
"awslogs-group": logGroupName,
"awslogs-stream-prefix": "batch"
}
}
}
}

/**
Expand Down
66 changes: 43 additions & 23 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const path = require('path');
const util = require('util');
const { getDockerLoginToECRCommand } = require('./awscli');

const DOCKER_TAG = `${Date.now()}`;

/**
* @returns {string} "<ECR Repo Name>:latest"
*/
function getDockerImageName() {
return `${this.provider.naming.getECRRepositoryURL()}:latest`;
return `${this.provider.naming.getECRRepositoryURL()}:${DOCKER_TAG}`;
}

/**
Expand All @@ -31,13 +33,17 @@ function dockerCommand(options, cwd = null, stdio = null) {
spawnOptions.stdio = stdio;
}

//console.error("dockerCommand:", options, spawnOptions);
const ps = spawnSync(cmd, options, spawnOptions);
if (ps.error) {
if (ps.error.code === 'ENOENT') {
throw new Error('docker not found! Please install it.');
}
throw new Error(ps.error);
} else if (ps.status !== 0) {
//console.error("docker output:", ps.output);
//console.error("docker stdout:", ps.stdout);
//console.error("docker stderr:", ps.stderr)
throw new Error(ps.stderr);
}
return ps;
Expand All @@ -51,50 +57,64 @@ function dockerCommand(options, cwd = null, stdio = null) {
* https://hub.docker.com/r/lambci/lambda/
*/
function buildDockerImage() {
this.serverless.cli.log(`Building docker image: "${this.provider.naming.getDockerImageName()}"...`);
let image = this.provider.naming.getDockerImageName();
this.serverless.cli.log(`Building docker image: "${image}"...`);

// Get any additional run commands we'd like to include in the docker image
let additionalRunCommands = "";
let addtionalBuildCommands = "";
if (this.serverless.service.custom
&& this.serverless.service.custom.awsBatch
&& this.serverless.service.custom.awsBatch.addtionalBuildCommands) {

_.forEach(
this.serverless.service.custom.awsBatch.addtionalBuildCommands,
runCommand => {
addtionalBuildCommands += `${runCommand}\n`;
}
)
}

let additionalCommands = "";
if (this.serverless.service.custom
&& this.serverless.service.custom.awsBatch
&& this.serverless.service.custom.awsBatch.additionalDockerRunCommands) {
&& this.serverless.service.custom.awsBatch.additionalCommands) {

_.forEach(
this.serverless.service.custom.awsBatch.additionalDockerRunCommands,
this.serverless.service.custom.awsBatch.additionalCommands,
runCommand => {
additionalRunCommands += `RUN ${runCommand}\n`;
additionalCommands += `${runCommand}\n`;
}
)
}

let buildImage = _.get(this.serverless.service,"custom.awsBatch.buildImage", "368461407828.dkr.ecr.us-east-1.amazonaws.com/build-batch-image-base:latest" );
let batchImage = _.get(this.serverless.service,"custom.awsBatch.batchImage", "368461407828.dkr.ecr.us-east-1.amazonaws.com/batch-image-base:latest" );

// Override some of the default lambci environmental variables.
// - Function Timeout and Function Memory are set by Env Variables on the Job Definition
// - Access Key and Secret are unset to that we use the role on the container that AWS provides
const dockerFileContents = `
FROM justinram11/lambda:build-${this.serverless.service.provider.runtime} AS builder
FROM ${buildImage} AS builder
USER root

${additionalRunCommands}
COPY ${this.serverless.service.service}.zip /tmp
RUN cd /tmp && unzip -q ${this.serverless.service.service}.zip && rm ${this.serverless.service.service}.zip

FROM justinram11/lambda:${this.serverless.service.provider.runtime}
COPY --from=builder /tmp /var/task/${this.serverless.service.service}/
RUN mkdir -p /tmp/task/
COPY ${this.serverless.service.service}.zip /tmp/task
RUN cd /tmp/task && unzip -q ${this.serverless.service.service}.zip && rm ${this.serverless.service.service}.zip
${addtionalBuildCommands}

FROM ${batchImage}
USER root
COPY --from=builder /tmp/task /var/task/
${additionalCommands}
RUN rm -rf /tmp/*
USER sbx_user1051
`;
// custom ENTRYPOINT due to heap setting - entrypoint copied from original
if (this.serverless.service.provider.runtime === 'nodejs8.10') dockerFileContents.concat(
'ENTRYPOINT ["/var/lang/bin/node", "--expose-gc", "--max-semi-space-size=150", "--max-old-space-size=30000", "/var/runtime/node_modules/awslambda/index.js"]\n'
);
if (this.serverless.service.provider.runtime === 'nodejs10.x') dockerFileContents.concat(
'ENV NODE_OPTIONS="--max-old-space-size=30000"\n'
);

const servicePath = this.serverless.config.servicePath || '';
const packagePath = path.join(servicePath || '.', '.serverless');
const dockerFile = path.join(packagePath, 'Dockerfile');
fse.writeFileSync(dockerFile, dockerFileContents);

const dockerOptions = ['build', '-f', dockerFile, '-t', this.provider.naming.getDockerImageName(), '.'];
const dockerOptions = ['build', '-f', dockerFile, '-t', image, '.'];
dockerCommand(dockerOptions, packagePath, 'inherit');

return BbPromise.resolve();
Expand All @@ -111,7 +131,7 @@ function pushDockerImageToECR() {

// Then perform the upload
this.serverless.cli.log("Uploading to ECR...");
dockerCommand(['push', this.provider.naming.getECRRepositoryURL()], null, 'inherit');
dockerCommand(['push', '--all-tags', this.provider.naming.getECRRepositoryURL()], null, 'inherit');

return BbPromise.resolve();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ecr.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getECRRepositoryName() {


/**
* @type {string} "<awsAccountID>.dkr.ecr.us-east-1.amazonaws.com/<serviceName>-<stage>"
* @type {string} "<awsAccountID>.dkr.ecr.us-east-1.amazonaws.com/<serviceName>-<stage>:<tag>"
*/

function getECRRepositoryURL() {
Expand Down
113 changes: 90 additions & 23 deletions lib/generateCoreTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,115 @@ const path = require('path');
const _ = require('lodash');
var util = require('util');

const lifeCyclePolicies = {
"rules": [
{
"rulePriority": 1,
"description": "Remove images over 90 days old",
"selection": {
"tagStatus": "any",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 90
},
"action": {
"type": "expire"
}
}
]
};

/**
* Adds the ECR repository to the "create" template so that we have a repository we can upload our docker image to
* during deployment.
*/
function generateCoreTemplate() {
// Setup our ECR Repository to delete untagged images after 1 day
const ecrTemplate = `
// Setup our ECR Repository to delete untagged images after 1 day
// const ecrTemplate = `
// {
// "Type" : "AWS::ECR::Repository",
// "Properties" : {
// "LifecyclePolicy" : {
// "LifecyclePolicyText" : ${JSON.stringify(JSON.stringify(lifeCyclePolicies))}
// },
// "RepositoryName" : "${this.provider.naming.getECRRepositoryName()}"
// }
// }
// `;
const ecrTemplate = `
{
"Type" : "AWS::ECR::Repository",
"Properties" : {
"LifecyclePolicy" : {
"LifecyclePolicyText" : "{\\"rules\\":[{\\"rulePriority\\":1,\\"description\\":\\"Remove untagged images\\",\\"selection\\":{\\"tagStatus\\":\\"untagged\\",\\"countType\\":\\"sinceImagePushed\\",\\"countUnit\\":\\"days\\",\\"countNumber\\":1},\\"action\\":{\\"type\\":\\"expire\\"}}]}"
},
"RepositoryName" : "${this.provider.naming.getECRRepositoryName()}"
}
}
`;

const newECRObject = {
[this.provider.naming.getECRLogicalId()]: JSON.parse(ecrTemplate)
};
const newECRObject = {
[this.provider.naming.getECRLogicalId()]: JSON.parse(ecrTemplate)
};

// Add it to our initial compiled cloud formation templates
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newECRObject
);
// Add it to our initial compiled cloud formation templates
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newECRObject
);

// Look for the serverless-log-forwarding custom parameters, if found, then forward the logs
// from our batch jobs to the same ARN...
const logGroupName = _.get(this.serverless.service,"custom.awsBatch.logGroupName");
const logDestinationArn = _.get(this.serverless.service,"custom.logForwarding.destinationARN" );
if ( logDestinationArn && logGroupName ) {
const batchLogGroupTemplate = {
"BatchLogGroupLambdaPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"FunctionName": logDestinationArn,
"Action": "lambda:InvokeFunction",
"Principal": "logs.us-east-1.amazonaws.com"
}
},
"BatchLogGroup": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": logGroupName,
"RetentionInDays": 7
}
},
"BatchLogGroupSubscriptionFilter": {
"Type": "AWS::Logs::SubscriptionFilter",
"Properties": {
"DestinationArn": logDestinationArn,
"FilterPattern": "",
"LogGroupName": logGroupName
},
"DependsOn": [
"BatchLogGroupLambdaPermission",
"BatchLogGroup"
]
}
}

_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
batchLogGroupTemplate
);
}

// Then write it back out to the file
const coreTemplateFileName = this.provider.naming.getCoreTemplateFileName();
// Then write it back out to the file
const coreTemplateFileName = this.provider.naming.getCoreTemplateFileName();

const coreTemplateFilePath = path.join(this.serverless.config.servicePath,
'.serverless',
coreTemplateFileName);
const coreTemplateFilePath = path.join(this.serverless.config.servicePath,
'.serverless',
coreTemplateFileName);

this.serverless.utils.writeFileSync(coreTemplateFilePath,
this.serverless.service.provider.compiledCloudFormationTemplate);
this.serverless.utils.writeFileSync(coreTemplateFilePath,
this.serverless.service.provider.compiledCloudFormationTemplate);

this.serverless.service.provider.coreCloudFormationTemplate =
_.cloneDeep(this.serverless.service.provider.compiledCloudFormationTemplate);
this.serverless.service.provider.coreCloudFormationTemplate =
_.cloneDeep(this.serverless.service.provider.compiledCloudFormationTemplate);

return BbPromise.resolve();
return BbPromise.resolve();
}

module.exports = { generateCoreTemplate };
Loading