Create a new EC2 instance. Args: name: Name tag for the instance. instance_type: EC2 instance type (e.g., 't2.micro'). ami_id: ID of the AMI to use. subnet_id: ID of the subnet to launch the instance in. security_g
(
self,
name: str,
instance_type: str,
ami_id: str,
subnet_id: Optional[str] = None,
security_group_ids: Optional[List[str]] = None,
key_name: Optional[str] = None,
user_data: Optional[str] = None,
block_device_mappings: Optional[List[Dict[str, Any]]] = None,
iam_instance_profile: Optional[str] = None,
tags: Optional[Dict[str, str]] = None,
wait: bool = True
)
| 106 | |
| 107 | @aws_operation("create_instance") |
| 108 | def create_instance( |
| 109 | self, |
| 110 | name: str, |
| 111 | instance_type: str, |
| 112 | ami_id: str, |
| 113 | subnet_id: Optional[str] = None, |
| 114 | security_group_ids: Optional[List[str]] = None, |
| 115 | key_name: Optional[str] = None, |
| 116 | user_data: Optional[str] = None, |
| 117 | block_device_mappings: Optional[List[Dict[str, Any]]] = None, |
| 118 | iam_instance_profile: Optional[str] = None, |
| 119 | tags: Optional[Dict[str, str]] = None, |
| 120 | wait: bool = True |
| 121 | ) -> Dict[str, Any]: |
| 122 | """ |
| 123 | Create a new EC2 instance. |
| 124 | |
| 125 | Args: |
| 126 | name: Name tag for the instance. |
| 127 | instance_type: EC2 instance type (e.g., 't2.micro'). |
| 128 | ami_id: ID of the AMI to use. |
| 129 | subnet_id: ID of the subnet to launch the instance in. |
| 130 | security_group_ids: List of security group IDs. |
| 131 | key_name: Name of the key pair to use. |
| 132 | user_data: String of user data to provide to the instance. |
| 133 | block_device_mappings: List of block device mappings. |
| 134 | iam_instance_profile: IAM instance profile name or ARN. |
| 135 | tags: Dictionary of tags to apply to the instance. |
| 136 | wait: Whether to wait for the instance to be running. |
| 137 | |
| 138 | Returns: |
| 139 | Details of the created instance. |
| 140 | """ |
| 141 | run_args = { |
| 142 | 'ImageId': ami_id, |
| 143 | 'InstanceType': instance_type, |
| 144 | 'MinCount': 1, |
| 145 | 'MaxCount': 1 |
| 146 | } |
| 147 | |
| 148 | # Add optional parameters |
| 149 | if subnet_id: |
| 150 | run_args['SubnetId'] = subnet_id |
| 151 | |
| 152 | if security_group_ids: |
| 153 | run_args['SecurityGroupIds'] = security_group_ids |
| 154 | |
| 155 | if key_name: |
| 156 | run_args['KeyName'] = key_name |
| 157 | |
| 158 | if user_data: |
| 159 | run_args['UserData'] = base64.b64encode(user_data.encode()).decode() |
| 160 | |
| 161 | if block_device_mappings: |
| 162 | run_args['BlockDeviceMappings'] = block_device_mappings |
| 163 | |
| 164 | if iam_instance_profile: |
| 165 | if iam_instance_profile.startswith('arn:'): |
no test coverage detected