AWS Service Catalog product launch using AWS SDK. Please see stacks configuration related
Configuring the service
We will setup lambda that launches the product configured in another article. Product name could be provided via input or environment variable. This tutorial uses environment variable
index.ts
import * as AWS from 'aws-sdk';
import {ProvisionProductInput} from 'aws-sdk/clients/servicecatalog';
import {APIGatewayProxyEvent} from 'aws-lambda';
const servicecatalog = new AWS.ServiceCatalog();
export async function handler() {
try {
const productName = process.env.SERVICE_CATALOG_PRODUCT_NAME || 'S3 Product';
const productDescription = await servicecatalog.describeProduct({
Name: productName
}).promise();
if (!productDescription) {
console.log(`productDescription is empty`);
return;
}
const artifactId = productDescription.ProvisioningArtifacts ? productDescription.ProvisioningArtifacts[0]?.Id : '';
const provisionResult = await servicecatalog.provisionProduct({
ProductId: productDescription.ProductViewSummary.ProductId,
ProvisionedProductName: `${productName}--Provisioned`,
ProvisioningArtifactId: artifactId,
} as ProvisionProductInput).promise();
} catch (e) {
console.log(`Error: ${JSON.stringify(e)}`);
return;
}
}
Product should be launched. If product is already launched (even if it's in Error state), an exception will be thrown
Providing stack parameters
Sometimes you would like to pass some parameters during stack provisioning.
Add to index.ts
const path = productDescription.LaunchPaths ? productDescription.LaunchPaths[0]?.Id : '';
const productParameters = await servicecatalog.describeProvisioningParameters({
ProductId: productDescription.ProductViewSummary.ProductId,
PathId: path,
ProvisioningArtifactId: artifactId
}).promise();
if (!productParameters) {
console.log(`productParameters are empty`);
return;
}
const param = productParameters.ProvisioningArtifactParameters.find(param => param.ParameterKey.includes('ParamName'));
And then add to provisionProduct request:
ProvisioningParameters: [
{Key: param.ParameterKey, Value: <ParamValue>}
]
Monitor product CF stack creation
You should get a ProvisionedProductId (from provisionProduct result or from AWS Web Console)
const description = await this.awsServiceCatalog.describeProvisionedProduct({ Id: productId }).promise();
console.log(`[*] product description: ${JSON.stringify(description)}`);
const provisionStatus = description?.ProvisionedProductDetail?.Status;
console.log(`[*] product status: ${JSON.stringify(provisionStatus)}`);
Comments
0 comments
Please sign in to leave a comment.