// Copied from https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk/lib/api/cloudformation-deployments.ts import * as cxapi from "@aws-cdk/cx-api"; import * as cdk_assets from "cdk-assets"; import { AssetManifest, IManifestEntry } from "cdk-assets"; import { Tag } from "sst-aws-cdk/lib/cdk-toolkit.js"; import { debug, warning } from "sst-aws-cdk/lib/logging.js"; import { buildAssets, publishAssets, BuildAssetsOptions, PublishAssetsOptions, PublishingAws, EVENT_TO_LOGGER, } from "sst-aws-cdk/lib/util/asset-publishing.js"; import { Mode } from "sst-aws-cdk/lib/api/aws-auth/credentials.js"; import { ISDK } from "sst-aws-cdk/lib/api/aws-auth/sdk.js"; import { CredentialsOptions, SdkForEnvironment, SdkProvider, } from "sst-aws-cdk/lib/api/aws-auth/sdk-provider.js"; import { deployStack, DeployStackResult, destroyStack, makeBodyParameterAndUpload, DeploymentMethod, } from "./deploy-stack.js"; import { loadCurrentTemplateWithNestedStacks, loadCurrentTemplate, } from "sst-aws-cdk/lib/api/nested-stack-helpers.js"; import { ToolkitInfo } from "sst-aws-cdk/lib/api/toolkit-info.js"; import { CloudFormationStack, Template, ResourcesToImport, ResourceIdentifierSummaries, } from "sst-aws-cdk/lib/api/util/cloudformation.js"; import { StackActivityProgress } from "sst-aws-cdk/lib/api/util/cloudformation/stack-activity-monitor.js"; import { replaceEnvPlaceholders } from "sst-aws-cdk/lib/api/util/placeholders.js"; import { callWithRetry } from "./util.js"; import { HotswapMode } from "sst-aws-cdk/lib/api/hotswap/common.js"; /** * SDK obtained by assuming the lookup role * for a given environment */ export interface PreparedSdkWithLookupRoleForEnvironment { /** * The SDK for the given environment */ readonly sdk: ISDK; /** * The resolved environment for the stack * (no more 'unknown-account/unknown-region') */ readonly resolvedEnvironment: cxapi.Environment; /** * Whether or not the assume role was successful. * If the assume role was not successful (false) * then that means that the 'sdk' returned contains * the default credentials (not the assume role credentials) */ readonly didAssumeRole: boolean; } export interface DeployStackOptions { /** * Stack to deploy */ readonly stack: cxapi.CloudFormationStackArtifact; /** * Execution role for the deployment (pass through to CloudFormation) * * @default - Current role */ readonly roleArn?: string; /** * Topic ARNs to send a message when deployment finishes (pass through to CloudFormation) * * @default - No notifications */ readonly notificationArns?: string[]; /** * Override name under which stack will be deployed * * @default - Use artifact default */ readonly deployName?: string; /** * Don't show stack deployment events, just wait * * @default false */ readonly quiet?: boolean; /** * Name of the toolkit stack, if not the default name * * @default 'CDKToolkit' */ readonly toolkitStackName?: string; /** * List of asset IDs which should NOT be built or uploaded * * @default - Build all assets */ readonly reuseAssets?: string[]; /** * Stack tags (pass through to CloudFormation) */ readonly tags?: Tag[]; /** * Stage the change set but don't execute it * * @default - true * @deprecated Use 'deploymentMethod' instead */ readonly execute?: boolean; /** * Optional name to use for the CloudFormation change set. * If not provided, a name will be generated automatically. * * @deprecated Use 'deploymentMethod' instead */ readonly changeSetName?: string; /** * Select the deployment method (direct or using a change set) * * @default - Change set with default options */ readonly deploymentMethod?: DeploymentMethod; /** * Force deployment, even if the deployed template is identical to the one we are about to deploy. * @default false deployment will be skipped if the template is identical */ readonly force?: boolean; /** * Extra parameters for CloudFormation * @default - no additional parameters will be passed to the template */ readonly parameters?: { [name: string]: string | undefined }; /** * Use previous values for unspecified parameters * * If not set, all parameters must be specified for every deployment. * * @default true */ readonly usePreviousParameters?: boolean; /** * Display mode for stack deployment progress. * * @default - StackActivityProgress.Bar - stack events will be displayed for * the resource currently being deployed. */ readonly progress?: StackActivityProgress; /** * Whether we are on a CI system * * @default false */ readonly ci?: boolean; /** * Rollback failed deployments * * @default true */ readonly rollback?: boolean; /* * Whether to perform a 'hotswap' deployment. * A 'hotswap' deployment will attempt to short-circuit CloudFormation * and update the affected resources like Lambda functions directly. * * @default - `HotswapMode.FULL_DEPLOYMENT` for regular deployments, `HotswapMode.HOTSWAP_ONLY` for 'watch' deployments */ readonly hotswap?: HotswapMode; /** * The extra string to append to the User-Agent header when performing AWS SDK calls. * * @default - nothing extra is appended to the User-Agent header */ readonly extraUserAgent?: string; /** * List of existing resources to be IMPORTED into the stack, instead of being CREATED */ readonly resourcesToImport?: ResourcesToImport; /** * If present, use this given template instead of the stored one * * @default - Use the stored template */ readonly overrideTemplate?: any; /** * Whether to build/publish assets in parallel * * @default true To remain backward compatible. */ readonly assetParallelism?: boolean; } interface AssetOptions { /** * Stack with assets to build. */ readonly stack: cxapi.CloudFormationStackArtifact; /** * Name of the toolkit stack, if not the default name. * * @default 'CDKToolkit' */ readonly toolkitStackName?: string; /** * Execution role for the building. * * @default - Current role */ readonly roleArn?: string; } export interface BuildStackAssetsOptions extends AssetOptions { /** * Options to pass on to `buildAsests()` function */ readonly buildOptions?: BuildAssetsOptions; /** * Stack name this asset is for */ readonly stackName?: string; } interface PublishStackAssetsOptions extends AssetOptions { /** * Options to pass on to `publishAsests()` function */ readonly publishOptions?: Omit; /** * Stack name this asset is for */ readonly stackName?: string; } export interface DestroyStackOptions { stack: cxapi.CloudFormationStackArtifact; deployName?: string; roleArn?: string; quiet?: boolean; force?: boolean; ci?: boolean; } export interface StackExistsOptions { stack: cxapi.CloudFormationStackArtifact; deployName?: string; } export interface DeploymentsProps { sdkProvider: SdkProvider; readonly quiet?: boolean; } /** * SDK obtained by assuming the deploy role * for a given environment */ export interface PreparedSdkForEnvironment { /** * The SDK for the given environment */ readonly stackSdk: ISDK; /** * The resolved environment for the stack * (no more 'unknown-account/unknown-region') */ readonly resolvedEnvironment: cxapi.Environment; /** * The Execution Role that should be passed to CloudFormation. * * @default - no execution role is used */ readonly cloudFormationRoleArn?: string; } /** * Scope for a single set of deployments from a set of Cloud Assembly Artifacts * * Manages lookup of SDKs, Bootstrap stacks, etc. */ export class Deployments { private readonly sdkProvider: SdkProvider; private readonly toolkitInfoCache = new Map(); private readonly sdkCache = new Map(); private readonly publisherCache = new Map< AssetManifest, cdk_assets.AssetPublishing >(); constructor(private readonly props: DeploymentsProps) { this.sdkProvider = props.sdkProvider; } public async readCurrentTemplateWithNestedStacks( rootStackArtifact: cxapi.CloudFormationStackArtifact, retrieveProcessedTemplate: boolean = false ): Promise