Last active
October 15, 2025 01:39
-
-
Save gabanox/9b18c5d13105f71d83fa31bc09a88e5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AWSTemplateFormatVersion: '2010-09-09' | |
| Description: | | |
| Infraestructura mínima en AWS para instancia EC2 en VPC por defecto, siguiendo requerimientos del README.md. | |
| Parameters: | |
| LatestAmiId: | |
| Type: String | |
| Default: ami-039a65ec6bd28e541 # ID AMI más reciente | |
| Description: AMI más reciente de Amazon Linux 2023. | |
| VpcId: | |
| Type: String | |
| Default: vpc-086fe118b4ed5c6e4 | |
| Description: ID de la VPC por defecto. | |
| SubnetId: | |
| Type: String | |
| Description: ID de la subred pública en la VPC por defecto. | |
| InstanceType: | |
| Type: String | |
| Default: t3.micro | |
| Description: Tipo de instancia EC2. | |
| InstanceName: | |
| Type: String | |
| Default: gabriel-awshero | |
| Description: Nombre de la instancia EC2. | |
| SecurityGroupId: | |
| Type: String | |
| Description: ID del grupo de seguridad por defecto de la VPC. | |
| LaunchTemplateName: | |
| Type: String | |
| Default: lt-gabriel | |
| Description: Nombre del Launch Template para EC2. | |
| AutoScalingGroupName: | |
| Type: String | |
| Default: asg-gabriel | |
| Description: Nombre del Auto Scaling Group. | |
| MinSize: | |
| Type: String | |
| Default: '1' | |
| Description: Tamaño mínimo del grupo de autoescalado. | |
| MaxSize: | |
| Type: String | |
| Default: '1' | |
| Description: Tamaño máximo del grupo de autoescalado. | |
| DesiredCapacity: | |
| Type: String | |
| Default: '1' | |
| Description: Capacidad deseada del grupo de autoescalado. | |
| Subnet1: | |
| Type: String | |
| Default: subnet-0f86fb485374f9f0a | |
| Description: ID de la primera subred pública. | |
| Subnet2: | |
| Type: String | |
| Default: subnet-0323a098b70caa778 | |
| Description: ID de la segunda subred pública. | |
| TagName: | |
| Type: String | |
| Default: Web Server - Gabriel | |
| Description: Valor de la etiqueta Name para instancias EC2. | |
| Resources: | |
| # Rol IAM mínimo para EC2 | |
| Ec2InstanceRole: | |
| Type: AWS::IAM::Role | |
| Properties: | |
| AssumeRolePolicyDocument: | |
| Version: '2012-10-17' | |
| Statement: | |
| - Effect: Allow | |
| Principal: | |
| Service: ec2.amazonaws.com | |
| Action: sts:AssumeRole | |
| Path: / | |
| ManagedPolicyArns: | |
| - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore # Permiso mínimo para administración | |
| Ec2InstanceProfile: | |
| Type: AWS::IAM::InstanceProfile | |
| Properties: | |
| Roles: | |
| - !Ref Ec2InstanceRole | |
| # Instancia EC2 en VPC por defecto | |
| Ec2Instance: | |
| Type: AWS::EC2::Instance | |
| Properties: | |
| InstanceType: !Ref InstanceType | |
| ImageId: !Ref LatestAmiId | |
| IamInstanceProfile: !Ref Ec2InstanceProfile | |
| NetworkInterfaces: | |
| - AssociatePublicIpAddress: true | |
| DeviceIndex: 0 | |
| SubnetId: !Ref SubnetId | |
| GroupSet: | |
| - !Ref SecurityGroupId | |
| Tags: | |
| - Key: Name | |
| Value: !Ref InstanceName | |
| UserData: | |
| Fn::Base64: !Sub | | |
| #!/bin/bash | |
| yum update -y | |
| yum install -y httpd php | |
| systemctl enable httpd | |
| systemctl start httpd | |
| PUBLIC_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) | |
| AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone) | |
| cat <<EOF > /var/www/html/index.php | |
| <?php | |
| echo "<h1>Instancia EC2 Apache + PHP</h1>"; | |
| echo "<p><b>IP Pública:</b> $PUBLIC_IP</p>"; | |
| echo "<p><b>Zona de disponibilidad:</b> $AZ</p>"; | |
| ?> | |
| EOF | |
| Ec2LaunchTemplate: | |
| Type: AWS::EC2::LaunchTemplate | |
| Properties: | |
| LaunchTemplateName: !Ref LaunchTemplateName | |
| LaunchTemplateData: | |
| ImageId: !Ref LatestAmiId | |
| InstanceType: !Ref InstanceType | |
| IamInstanceProfile: | |
| Arn: !GetAtt Ec2InstanceProfile.Arn | |
| SecurityGroupIds: | |
| - !Ref SecurityGroupId | |
| TagSpecifications: | |
| - ResourceType: instance | |
| Tags: | |
| - Key: Name | |
| Value: !Ref TagName | |
| Ec2AutoScalingGroup: | |
| Type: AWS::AutoScaling::AutoScalingGroup | |
| Properties: | |
| AutoScalingGroupName: !Ref AutoScalingGroupName | |
| VPCZoneIdentifier: | |
| - !Ref Subnet1 | |
| - !Ref Subnet2 | |
| LaunchTemplate: | |
| LaunchTemplateId: !Ref Ec2LaunchTemplate | |
| Version: !GetAtt Ec2LaunchTemplate.LatestVersionNumber | |
| MinSize: !Ref MinSize | |
| MaxSize: !Ref MaxSize | |
| DesiredCapacity: !Ref DesiredCapacity | |
| HealthCheckType: EC2 | |
| HealthCheckGracePeriod: 300 | |
| Tags: | |
| - Key: Name | |
| Value: !Ref TagName | |
| PropagateAtLaunch: true | |
| Outputs: | |
| Ec2InstanceId: | |
| Description: ID de la instancia EC2 creada. | |
| Value: !Ref Ec2Instance | |
| Ec2PublicIp: | |
| Description: IP pública de la instancia EC2. | |
| Value: !GetAtt Ec2Instance.PublicIp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment