当前位置: 首页>>代码示例>>C#>>正文


C# RunInstancesRequest.WithUserData方法代码示例

本文整理汇总了C#中Amazon.EC2.Model.RunInstancesRequest.WithUserData方法的典型用法代码示例。如果您正苦于以下问题:C# RunInstancesRequest.WithUserData方法的具体用法?C# RunInstancesRequest.WithUserData怎么用?C# RunInstancesRequest.WithUserData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Amazon.EC2.Model.RunInstancesRequest的用法示例。


在下文中一共展示了RunInstancesRequest.WithUserData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LaunchInstance

        public void LaunchInstance(string instanceName, InstanceSize size, string metadata)
        {
            var ec2Client = new AmazonEC2Client(_awsCredentials);

            string instanceType = "t1.micro";
            // WEB: string imageId = "ami-a9da0ec0";
            // Compute: string imageId = "ami-443fe32d";
            string imageId = "ami-09d96e60";

            switch (size)
            {
                case InstanceSize.Medium:
                    instanceType = "t1.small";
                    break;

                case InstanceSize.Small:
                default:
                    instanceType = "t1.micro";
                    break;
            }

            var request = new RunInstancesRequest()
                .WithInstanceType(instanceType)
                .WithPlacement(new Placement().WithAvailabilityZone("us-east-1d"))
                .WithImageId(imageId)
                .WithMinCount(1)
                .WithMaxCount(1)
                .WithSecurityGroup("default")
                .WithKeyName("IgorTest3");

            if (!string.IsNullOrEmpty(metadata))
                request = request.WithUserData(Convert.ToBase64String(Encoding.UTF8.GetBytes(metadata)));

            var runInstancesResponse = ec2Client.RunInstances(request);

            var instances = runInstancesResponse.RunInstancesResult.Reservation.RunningInstance;
            var index = 0;
            foreach (var instance in instances)
            {
                var name = instanceName;

                if (instances.Count > 0)
                    instanceName = instanceName + index;

                var createTagsRequest = new CreateTagsRequest();
                createTagsRequest
                    .WithResourceId(instance.InstanceId)
                    .WithTag(new Tag().WithKey("Name")
                    .WithValue(name));
                ec2Client.CreateTags(createTagsRequest);

                index++;
            }

            /*
            string rsaPrivateKey;
            using (var reader = new StreamReader(@"C:\Dev\BlueMetal\CloudAbstractions\SecurityStorage\IgorKeyPair.pem"))
            {
                rsaPrivateKey = reader.ReadToEnd();
            }
            var result = ec2Client.GetPasswordData(
                        new GetPasswordDataRequest().WithInstanceId(instanceId))
                .GetPasswordDataResult;

            Console.WriteLine(result.GetDecryptedPassword(rsaPrivateKey));
            */

            /*
                        AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(
                          appConfig["AWSAccessKey"],
                          appConfig["AWSSecretKey"],
                          new AmazonEC2Config().WithServiceURL("https://eu-west-1.ec2.amazonaws.com")
                          );

                        DescribeInstancesRequest ec2Request = new DescribeInstancesRequest();
            */
        }
开发者ID:CloudMorph,项目名称:CloudMorph,代码行数:77,代码来源:AwsComputeInfrastructureProvider.cs


注:本文中的Amazon.EC2.Model.RunInstancesRequest.WithUserData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。