Esempi cloudformation JSON files.

Tre Esempi Spiegati di aws cloudformation update-stack con JSON


Esempio 1: Aggiornamento di un'istanza EC2 con un nuovo tipo di macchina

Comando:

aws cloudformation update-stack --stack-name EvolutiveStack --template-body file://update-ec2.json

Template JSON (update-ec2.json):

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "MyEC2Instance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "InstanceType": "t3.medium",
                "ImageId": "ami-1234567890abcdef0",
                "Tags": [
                    { "Key": "Name", "Value": "EvolutiveInstance" }
                ]
            }
        }
    }
}

Spiegazione

  1. Modifica il tipo di istanza di una VM EC2 già esistente.

    • "InstanceType": "t3.medium"
  2. Usa un'AMI specifica per creare l'istanza EC2.

    • "ImageId": "ami-1234567890abcdef0"
  3. Aggiorna il nome dell'istanza tramite i tag AWS.

    • "Tags": [{ "Key": "Name", "Value": "EvolutiveInstance" }]
  4. CloudFormation aggiornerà solo la risorsa modificata senza ricreare l’intero stack.

    • update-stack

Esempio 2: Aggiornamento di un bilanciatore di carico con un nuovo target group

Comando:

aws cloudformation update-stack --stack-name EvolutiveStack --template-body file://update-alb.json

Template JSON (update-alb.json):

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "MyLoadBalancer": {
            "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
            "Properties": {
                "Name": "EvolutiveALB",
                "Scheme": "internet-facing",
                "LoadBalancerAttributes": [
                    {
                        "Key": "deletion_protection.enabled",
                        "Value": "true"
                    }
                ],
                "Subnets": ["subnet-12345abc", "subnet-67890def"]
            }
        },
        "MyTargetGroup": {
            "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
            "Properties": {
                "Name": "EvolutiveTargetGroup",
                "Protocol": "HTTP",
                "Port": 80,
                "VpcId": "vpc-09876xyz"
            }
        }
    }
}

Spiegazione

  1. Aggiorna un Application Load Balancer (ALB) esistente con un nuovo target group.

    • "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer"
  2. Modifica le subnet per ridistribuire il traffico su diverse zone di disponibilità.

    • "Subnets": ["subnet-12345abc", "subnet-67890def"]
  3. Aggiunge protezione contro la cancellazione accidentale.

    • "deletion_protection.enabled", "Value": "true"
  4. Crea un nuovo target group per instradare il traffico in un VPC specifico.

    • "VpcId": "vpc-09876xyz"

Esempio 3: Aggiornamento di un bucket S3 con nuova configurazione di versioning

Comando:

aws cloudformation update-stack --stack-name EvolutiveStack --template-body file://update-s3.json

Template JSON (update-s3.json):

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "MyS3Bucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": "evolutive-storage",
                "VersioningConfiguration": {
                    "Status": "Enabled"
                },
                "AccessControl": "Private"
            }
        }
    }
}

Spiegazione

  1. Abilita la gestione delle versioni in un bucket S3 esistente.

    • "VersioningConfiguration": { "Status": "Enabled" }
  2. Aggiorna il nome del bucket per supportare nuovi dati evolutivi.

    • "BucketName": "evolutive-storage"
  3. Imposta i permessi del bucket su privato per maggiore sicurezza.

    • "AccessControl": "Private"
  4. CloudFormation aggiorna solo la configurazione senza eliminare il bucket esistente.

    • update-stack