2024-09-26 14:54:18 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[codepipeline.javascript.MyCodePipelineFunction.complete]
2024-10-16 11:53:18 -04:00
const assert = require ( "node:assert" ) ;
const AWS = require ( "aws-sdk" ) ;
const http = require ( "node:http" ) ;
2024-09-26 14:54:18 -04:00
2024-10-16 11:53:18 -04:00
exports . handler = ( event , context ) => {
const codepipeline = new AWS . CodePipeline ( ) ;
2024-09-26 14:54:18 -04:00
// Retrieve the Job ID from the Lambda action
2024-10-16 11:53:18 -04:00
const jobId = event [ "CodePipeline.job" ] . id ;
2024-09-26 14:54:18 -04:00
// Retrieve the value of UserParameters from the Lambda action configuration in AWS CodePipeline, in this case a URL which will be
// health checked by this function.
2024-10-16 11:53:18 -04:00
const url =
2024-09-26 14:54:18 -04:00
event [ "CodePipeline.job" ] . data . actionConfiguration . configuration
. UserParameters ;
// Notify AWS CodePipeline of a successful job
2024-10-16 11:53:18 -04:00
const putJobSuccess = ( message ) => {
const params = {
2024-09-26 14:54:18 -04:00
jobId : jobId ,
} ;
2024-10-16 11:53:18 -04:00
codepipeline . putJobSuccessResult ( params , ( err , data ) => {
2024-09-26 14:54:18 -04:00
if ( err ) {
context . fail ( err ) ;
} else {
context . succeed ( message ) ;
}
} ) ;
} ;
// Notify AWS CodePipeline of a failed job
2024-10-16 11:53:18 -04:00
const putJobFailure = ( message ) => {
const params = {
2024-09-26 14:54:18 -04:00
jobId : jobId ,
failureDetails : {
message : JSON . stringify ( message ) ,
type : "JobFailed" ,
externalExecutionId : context . invokeid ,
} ,
} ;
2024-10-16 11:53:18 -04:00
codepipeline . putJobFailureResult ( params , ( err , data ) => {
2024-09-26 14:54:18 -04:00
context . fail ( message ) ;
} ) ;
} ;
// Validate the URL passed in UserParameters
if ( ! url || url . indexOf ( "http://" ) === - 1 ) {
putJobFailure (
"The UserParameters field must contain a valid URL address to test, including http:// or https://" ,
) ;
return ;
}
// Helper function to make a HTTP GET request to the page.
// The helper will test the response and succeed or fail the job accordingly
2024-10-16 11:53:18 -04:00
const getPage = ( url , callback ) => {
const pageObject = {
2024-09-26 14:54:18 -04:00
body : "" ,
statusCode : 0 ,
contains : function ( search ) {
return this . body . indexOf ( search ) > - 1 ;
} ,
} ;
http
2024-10-16 11:53:18 -04:00
. get ( url , ( response ) => {
2024-09-26 14:54:18 -04:00
pageObject . body = "" ;
pageObject . statusCode = response . statusCode ;
2024-10-16 11:53:18 -04:00
response . on ( "data" , ( chunk ) => {
2024-09-26 14:54:18 -04:00
pageObject . body += chunk ;
} ) ;
2024-10-16 11:53:18 -04:00
response . on ( "end" , ( ) => {
2024-09-26 14:54:18 -04:00
callback ( pageObject ) ;
} ) ;
response . resume ( ) ;
} )
2024-10-16 11:53:18 -04:00
. on ( "error" , ( error ) => {
2024-09-26 14:54:18 -04:00
// Fail the job if our request failed
putJobFailure ( error ) ;
} ) ;
} ;
2024-10-16 11:53:18 -04:00
getPage ( url , ( returnedPage ) => {
2024-09-26 14:54:18 -04:00
try {
// Check if the HTTP response has a 200 status
assert ( returnedPage . statusCode === 200 ) ;
// Check if the page contains the text "Congratulations"
// You can change this to check for different text, or add other tests as required
assert ( returnedPage . contains ( "Congratulations" ) ) ;
// Succeed the job
putJobSuccess ( "Tests passed." ) ;
} catch ( ex ) {
// If any of the assertions failed then fail the job
putJobFailure ( ex ) ;
}
} ) ;
} ;
// snippet-end:[codepipeline.javascript.MyCodePipelineFunction.complete]