1. Using aws servicecatalog with vpc

    TypeScript

    To use AWS Service Catalog with VPCs in Pulumi, you typically want to create a Service Catalog product that provisions an AWS VPC, and then instantiate that product for use. AWS Service Catalog allows organizations to create and manage catalogs of IT services that are approved for use on AWS. These services can include everything from virtual machine images, servers, software, and databases to complete multi-tier application architectures.

    In this guide, I'll walk you through creating a basic AWS VPC using Pulumi's AWS provider, and then we'll create a Service Catalog CloudFormation template for a product that includes the VPC. We'll also use Pulumi's AWS Service Catalog support to define the product and provisioning artifact (version) in the service catalog. Finally, we'll instantiate the product.

    Here's a step-by-step Pulumi TypeScript program that completes this task:

    1. Create a VPC - Define a VPC with basic configurations.
    2. CloudFormation Template - Prepare a CloudFormation template for AWS Service Catalog, describing the VPC resource.
    3. Service Catalog Product - Create a Service Catalog product using the prepared CloudFormation template.
    4. Provisioning Artifact - Add a provisioning artifact (version) to the Service Catalog product.
    5. Provisioned Product - Instantiate the Service Catalog product as a provisioned product which actually deploys the VPC.

    Let's start with setting up our Pulumi program with the necessary imports and basic setup:

    import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; import * as awsx from "@pulumi/awsx"; // Define the VPC using AWSX, which provides a higher level abstraction to simplify VPC creation. const vpc = new awsx.ec2.Vpc("customVpc", { numberOfAvailabilityZones: 2, }); // Output the VPC ID and subnets, which can be used in other resources or outputs export const vpcId = vpc.id; export const publicSubnetIds = vpc.publicSubnetIds; export const privateSubnetIds = vpc.privateSubnetIds;

    In this part of the program, we use awsx.ec2.Vpc to create a new VPC across 2 availability zones for high availability. This module simplifies creating a VPC compared to the lower-level aws.ec2.Vpc resource. We also export the VPC ID and subnet IDs which could be used for further provisioning or output.

    Next, we'll create a CloudFormation template which is necessary for defining a Service Catalog product. Since the CloudFormation capabilities in AWS Service Catalog are quite extensive, I'll keep this step simple for the purpose of this explanation:

    const vpcCloudFormationTemplate = `{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "CloudFormation for VPC to be used in Service Catalog", "Resources": { "VPC": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock": "10.0.0.0/16", "EnableDnsSupport": "true", "EnableDnsHostnames": "true" } } } }`; // Define a Service Catalog product with the given CloudFormation template const serviceCatalogProduct = new aws.servicecatalog.Product("vpcProduct", { name: "VPCProduct", owner: "product-owner@example.com", productType: "CLOUD_FORMATION_TEMPLATE", provisioningArtifactParameters: [{ info: { LoadTemplateFromURL: pulumi.interpolate`data:;,${encodeURIComponent(vpcCloudFormationTemplate)}`, }, name: "v1", type: "CLOUD_FORMATION_TEMPLATE", }] }); // A real provisioning would involve the user selecting a provisioned product and configuration // Here, we will just provide a skeleton structure that could be expanded upon const provisionedProduct = new aws.servicecatalog.ProvisionedProduct("vpcProvisionedProduct", { productId: serviceCatalogProduct.id, provisioningArtifactId: serviceCatalogProduct.provisioningArtifactParameters[0].id, provisioningParameters: [{ key: "VpcCidr", // Assume our CFN template is expecting this parameter value: "10.0.0.0/16" }], }); // Output the provisioned product ID export const provisionedProductId = provisionedProduct.id;

    In this part, we create a string containing a JSON representation of a CloudFormation template for provisioning a VPC. This is a very basic example, and in a production scenario, you would likely have a more complex CloudFormation template that is loaded from a separate file or URL.

    We then define a Service Catalog Product and a ProvisionedProduct. The product represents the catalog entry of the CloudFormation template we constructed. The provisioned product is an instance of this product that, when deployed, results in the resources defined in the CloudFormation template being created.

    Note that, in a production environment, provisioning parameters can be dynamically passed based on user input or other runtime data. Here, we hardcode the VpcCidr parameter for simplicity.

    This is an introductory program to AWS Service Catalog and AWS VPC creation with Pulumi. For a full-fledged solution, you would need to expand the CloudFormation template, handle parameters dynamically, and probably integrate with other AWS services.