faint-elephant-30784
10/01/2024, 12:39 PMimport pulumi
import pulumi_command as command
import pulumi_tls as tls
import pulumi_aws as aws
import pulumi_aws_native as aws_native
from typing import (
Mapping,
List,
) # I have Imported this Library to keep consistency to other pkgs and also to import base tags properly.
import cority_aws_compute_ec2_securitygroups as secgroup
class CorityAwsComputeEc2InstanceArgs:
def __init__(
self,
# Metadata
name: str,
description: str,
base_tags: Mapping[str, str],
type: str,
enable_deletion_protection: bool,
ami_id: str,
purpose: str,
ebs_block_device_mappings,
# key_pair_name,
# instance_profile
# Networking
vpc_id: str,
subnet_id: str,
security_groups: List[str],
key_name: str,
instance_profile=None,
volumes=None,
# role_override
):
# Investigate whether these parameters can be declared as class properties.
# VALIDATE ARGUMENTS
if not vpc_id.startswith("vpc-"):
raise ValueError(
'The vpc_id value does not match the aws format starting with "vpc-"'
)
self.type = type
self.enable_deletion_protection = enable_deletion_protection
self.ami_id = ami_id
self.name = name
self.description = description
self.base_tags = base_tags
self.vpc_id = vpc_id
self.subnet_id = subnet_id
self.security_groups = security_groups
self.instance_profile = instance_profile
self.volumes = volumes
self.key_name = key_name
self.ec2_az = aws.ec2.get_subnet(id=subnet_id).availability_zone
for volume in self.volumes:
if not "availability_zone" in volume["properties"]:
volume["properties"]["availability_zone"] = self.ec2_az
self.ebs_block_device_mappings = ebs_block_device_mappings
if purpose == "storage_gateway":
allowed_instance_types = [
"t2.medium",
"t2.large",
"t2.xlarge",
"t3.medium",
"t3a.medium",
"t3.large",
"t3.2xlarge",
"t3a.large",
"t3a.xlarge",
"m5.2xlarge",
"m5.xlarge",
"r6i.large",
"r6i.xlarge",
"r6i.2xlarge",
"r6i.4xlarge",
"r6i.8xlarge",
"r6i.12xlarge",
]
if not type in allowed_instance_types:
raise ValueError(
'The "'
+ type
+ '" instance type is now within the list of allowed storage gateway instance types.'
)
class CorityAwsComputeEc2Instance(pulumi.ComponentResource):
def __init__(
self, resource_name: str, args: CorityAwsComputeEc2InstanceArgs, opts=None
):
super().__init__("aws:compute:ec2instance", resource_name, None, opts)
self.instance = aws_native.ec2.Instance(
resource_name=resource_name,
image_id=args.ami_id,
instance_type=args.type,
security_group_ids=args.security_groups,
subnet_id=args.subnet_id,
block_device_mappings=args.ebs_block_device_mappings,
# iam_instance_profile = args.instance_profile,
key_name=args.key_name,
opts=opts,
)
self.volume_attachments = []
for volume in args.volumes:
volume["properties"]["resource_name"] = volume["resource_suffix"]
volume_properties = volume["properties"]
volume_resource = aws_native.ec2.Volume(**volume_properties)
volume["attachment"]["resource_name"] = (
"attachment-" + volume["resource_suffix"]
)
volume["attachment"]["instance_id"] = self.instance.id.apply(lambda v: v)
volume["attachment"]["volume_id"] = volume_resource.id.apply(lambda v: v)
volume["attachment"]["opts"] = pulumi.ResourceOptions(
depends_on=[self.instance, volume_resource]
)
attachment = aws_native.ec2.VolumeAttachment(**volume["attachment"])
self.volume_attachments.append(attachment)
self.register_outputs(
{
"instance_id": self.instance.id,
"volume_attachments": self.volume_attachments,
}
)