Skip to content

Commit

Permalink
feat(lambda): use artillery-hosted ECR image (#2724)
Browse files Browse the repository at this point in the history
* feat(lambda): use artillery-hosted ECR image

* test(lambda): add a smoke test for container lambda

* fix(lambda): use old naming to maintain iam backwards compatibility

* fix(lambda): use correct ECR repo
  • Loading branch information
bernardobridge committed May 13, 2024
1 parent 81717f9 commit 129c69a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 48 deletions.
51 changes: 5 additions & 46 deletions packages/artillery/lib/platform/aws-lambda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ class PlatformLambda {
const platformConfig = platformOpts.platformConfig;

this.currentVersion = process.env.LAMBDA_IMAGE_VERSION || pkgVersion;
this.ecrImageRepository =
process.env.LAMBDA_IMAGE_REPOSITORY || 'artillery-worker';
this.ecrImageUrl = process.env.WORKER_IMAGE_URL;
this.isContainerLambda = platformConfig.container === 'true';
this.architecture = platformConfig.architecture || 'arm64';
this.region = platformConfig.region || 'us-east-1';
Expand Down Expand Up @@ -138,7 +137,6 @@ class PlatformLambda {

let bom, s3Path;
if (this.isContainerLambda) {
await this.ensureECRImageExists();
const result = await createAndUploadTestDependencies(
this.bucketName,
this.testRunId,
Expand Down Expand Up @@ -227,7 +225,7 @@ class PlatformLambda {

let shouldCreateLambda;
if (this.isContainerLambda) {
this.functionName = `artillery-worker-v${this.currentVersion.replace(
this.functionName = `artilleryio-v${this.currentVersion.replace(
/\./g,
'-'
)}`;
Expand Down Expand Up @@ -628,47 +626,6 @@ class PlatformLambda {
return lambdaRoleArn;
}

async ensureECRImageExists() {
const ecr = new AWS.ECR({ apiVersion: '2015-09-21', region: this.region });

const params = {
repositoryName: this.ecrImageRepository,
imageIds: [{ imageTag: this.currentVersion }]
};

let data;
try {
data = await ecr.describeImages(params).promise();
} catch (err) {
if (err.code === 'RepositoryNotFoundException') {
//TODO: add links to ECR documentation when available
console.error(
`ECR repository not found: ${this.ecrImageRepository}. Have you created an ECR Private Repository in ${this.region}?`
);
process.exit(1);
}

if (err.code === 'ImageNotFoundException') {
//TODO: add links to ECR documentation when available
console.error(
`ECR image ${this.currentVersion} not found in repository ${this.ecrImageRepository}. Have you pushed the image to ECR?`
);
process.exit(1);
}

throw new Error(`Unexpected error getting ECR image:\n${err}`);
}

if (data.imageDetails[0].imageTags.includes(this.currentVersion)) {
return;
} else {
console.error(
`ECR image ${this.currentVersion} not found in repository ${this.ecrImageRepository}. Have you pushed the image to ECR?`
);
process.exit(1);
}
}

async checkIfNewLambdaIsNeeded({ memorySize, functionName }) {
const lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
Expand Down Expand Up @@ -712,7 +669,9 @@ class PlatformLambda {
lambdaConfig = {
PackageType: 'Image',
Code: {
ImageUri: `${this.accountId}.dkr.ecr.${this.region}.amazonaws.com/${this.ecrImageRepository}:${this.currentVersion}`
ImageUri:
this.ecrImageUrl ||
`248481025674.dkr.ecr.${this.region}.amazonaws.com/artillery-worker:${this.currentVersion}`
},
ImageConfig: {
Command: ['a9-handler-index.handler'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
scenarios:
- flow:
- beforeScenario: emitCsvCounters
flow:
- loop:
- get:
url: "/dino"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = { maybeSleep };
module.exports = { maybeSleep, emitCsvCounters };

function maybeSleep(req, context, events, done) {
if (Math.random() < 0.7) {
Expand All @@ -9,3 +9,15 @@ function maybeSleep(req, context, events, done) {
return done();
}, Math.random() * 1000);
}

function emitCsvCounters(context, events, done) {
if (context.vars.number) {
events.emit('counter', `csv_number_${context.vars.number}`, 1);
}

if (context.vars.name) {
events.emit('counter', `csv_name_${context.vars.name}`, 1);
}

return done();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const tap = require('tap');
const { $ } = require('zx');
const { getTestTags } = require('../../cli/_helpers.js');

tap.test('Run a test on AWS Lambda using containers', async (t) => {
const tags = getTestTags(['type:acceptance']);
const configPath = `${__dirname}/fixtures/quick-loop-with-csv/config.yml`;
const scenarioPath = `${__dirname}/fixtures/quick-loop-with-csv/blitz.yml`;

process.env.LAMBDA_IMAGE_VERSION = process.env.ECR_IMAGE_VERSION;

const output =
await $`artillery run-lambda --count 10 --region us-east-1 --container --config ${configPath} --record --tags ${tags} ${scenarioPath}`;

t.equal(output.exitCode, 0, 'CLI should exit with code 0');

t.ok(
output.stdout.indexOf('Summary report') > 0,
'Should print summary report'
);
t.ok(
output.stdout.indexOf('http.codes.200') > 0,
'Should print http.codes.200'
);

t.ok(
output.stdout.indexOf('csv_number_') > 0,
'Should print csv_number_ counters'
);

t.ok(
output.stdout.indexOf('csv_name_') > 0,
'Should print csv_name_ counters'
);
});
10 changes: 10 additions & 0 deletions packages/artillery/test/cloud-e2e/lambda/run-lambda.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ tap.test('Run a test on AWS Lambda', async (t) => {
output.stdout.indexOf('http.codes.200') > 0,
'Should print http.codes.200'
);

t.ok(
output.stdout.indexOf('csv_number_') > 0,
'Should print csv_number_ counters'
);

t.ok(
output.stdout.indexOf('csv_name_') > 0,
'Should print csv_name_ counters'
);
});

0 comments on commit 129c69a

Please sign in to comment.