Pépin Rémi, Ensai, 2025
remi.pepin@insee.fr
Machine unique
Flotte de machines
from constructs import Construct
from cdktf import App, NamedRemoteWorkspace, TerraformStack, TerraformOutput, RemoteBackend
from cdktf_cdktf_provider_aws.provider import AwsProvider
from cdktf_cdktf_provider_aws.instance import Instance, InstanceEbsBlockDevice
class MyStack(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)
AwsProvider(self, "AWS", region="us-west-1")
instance = Instance(self, "compute",
ami="ami-01456a894f71116f2", # l'id de l'os. Pas possible de juste mettre ubuntu
instance_type="t2.micro", # le type de l'instance
ebs_block_device= [InstanceEbsBlockDevice(
device_name="/dev/sda1",
delete_on_termination=True,
encrypted=False,
volume_size=20,
volume_type="gp2"
))
TerraformOutput(self, "public_ip",
value=instance.public_ip,
)
app = App()
stack = MyStack(app, "aws_instance")
app.synth()
Plus qu'a faire un "cdktf deploy" pour déployer l'architecture
class MyStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "AWS", region="us-east-1")
security_group = SecurityGroup(self, "sg-tp",...)
launch_template = LaunchTemplate(self, "compute",...)
target_group = LbTargetGroup(self, "target_group",...)
elb = Lb(self, "ELB",...)
lb_listener = LbListener(self, "lb_listener",...)
asg = AutoscalingGroup(self,"asg",...)
app = App()
MyStack(app, "cloud_commputing")
app.synth()
class HighAvailabilityStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "AWS", region="us-east-1")
security_group = SecurityGroup(self, "sg-tp")
launch_template = LaunchTemplate(self, "compute",...)
target_group = LbTargetGroup(self, "target_group",...)
elb = Lb(self, "ELB",...)
lb_listener = LbListener(self, "lb_listener", ...)
asg = AutoscalingGroup(self,"asg", ...)
app = App()
MyStack(app, "HighAvailabilityApp")
app.synth()
Ce que vous allez faire