Lesson 2 of 5 ML Infrastructure and Containers

Provisioning ML Infrastructure with Infrastructure as Code

This lesson explains how infrastructure as code creates the many resources of an ML system repeatably across accounts and passes their identifiers to the services that use them.

Learning outcomes

After completing this lesson, you will be able to:

  • Define resources with CloudFormation and the CDK.
  • Share values between stacks and services.
  • Provision standardised environments with SageMaker Projects.

CloudFormation and CDK

AWS CloudFormation declares resources in YAML or JSON templates and deploys them as stacks, with change sets to preview updates and automatic rollback on failure. It has resource types for SageMaker AI models, endpoint configurations, endpoints, pipelines, feature groups, model package groups and Bedrock knowledge bases, agents and guardrails. The AWS CDK defines the same resources in Python or TypeScript and synthesizes CloudFormation, which lets teams build reusable constructs such as "a monitored endpoint with alarms".

Communication between stacks and services

Large systems are split into stacks, such as network, data, training and serving. Stacks share values through outputs and exports consumed with Fn::ImportValue, which creates a hard dependency; through nested stacks; or through SSM Parameter Store parameters, which decouple stacks so one can be replaced without blocking the other. Orchestration services consume these values: a Step Functions state machine or SageMaker pipeline receives an execution role ARN, subnet IDs or a model package group name as parameters; EventBridge rules connect stacks through events, such as a model approval starting a deployment.

Match the pairs

Connect the stacks

Exam skills 3.2.2

Match each integration need with the mechanism that provides it.

Log in to try this activity Interactive activities are free with an account.

Standardized ML environments

SageMaker Projects use AWS Service Catalog products to provision a complete MLOps setup from a template: code repositories, a model-building pipeline, a Model Registry group and CI/CD pipelines that deploy to staging and production. A data science team starting a new project receives the whole structure in minutes instead of building it by hand.

Platform teams publish approved templates, which encode the organisation's standards for security, networking and deployment, so every team's infrastructure is compliant from the start. The following table summarises the integration methods.

MethodCouplingUse when
Outputs and Fn::ImportValueTight (export cannot change while imported)Stable shared values such as VPC IDs
Nested stacksParent manages childrenComponents deployed together
SSM Parameter StoreLooseValues read by stacks, pipelines and code
Pipeline or state machine parametersRuntimePassing ARNs and names into orchestration
SageMaker Projects + Service CatalogTemplateStandard MLOps environments for many teams

The following diagram shows how values pass between three separate infrastructure stacks, network, serving and training, without tight coupling.

Loose coupling between the three stacks. The network stack writes the private subnet IDs to a Parameter Store parameter; the serving stack reads it at deploy time; and the training pipeline receives the values as run-time parameters. The network team can change its stack without first removing imports from the ML stacks.
The scenario is illustrative.
True or false

Standard environments

Exam skills 3.2.2

Check how infrastructure as code standardises ML environments.

Log in to try this activity Interactive activities are free with an account.
Worked example

Three stacks

A team splits its ML platform into a network stack, a training stack and a serving stack, deployed per environment.

# network stack (CDK, Python)
ssm.StringParameter(self, "PrivateSubnets", parameter_name="/ml/prod/private-subnets",
                    string_value=",".join(s.subnet_id for s in vpc.private_subnets))

# serving stack reads the value at deploy time
subnets = ssm.StringParameter.value_for_string_parameter(self, "/ml/prod/private-subnets")

# training pipeline receives values as parameters at run time
pipeline.start(parameters={"SubnetIds": subnets, "ModelPackageGroup": "churn-models"})

Parameter Store keeps the stacks loosely coupled, so the network team can update its stack without first removing imports from the ML stacks. Hard-coding subnet IDs in each template would break the moment the network changed.

Check yourself

How can stacks share values loosely, without a hard dependency?

Show answer

Through SSM Parameter Store parameters.

Exam Tips

Remember for the exam

  • Repeatable ML infrastructure: CloudFormation or CDK.
  • Share values loosely between stacks: SSM Parameter Store.
  • Tight sharing of stable values: outputs with Fn::ImportValue.
  • Pass ARNs and IDs into Step Functions or SageMaker Pipelines as parameters.
  • Standard MLOps templates for many teams: SageMaker Projects with Service Catalog.