【问题标题】:Setting up IIS on EC2 with CloudFormation使用 CloudFormation 在 EC2 上设置 IIS
【发布时间】:2021-05-30 23:37:42
【问题描述】:

我正在学习如何利用 AWS 及其资源。当前尝试使用 JSON 格式的 CloudFormation 在 EC2 实例上设置 IIS。我不确定我的userData 段以及是否可能在 IIS EC2 实例的其他段中丢失任何内容。当我使用 cloudFormation 部署此脚本时,成功创建了一个堆栈,但是,测试生成的实例的远程桌面以崩溃结束,这可能是什么原因?

我的代码-

{
"AWSTemplateFormatVersion": "2010-09-09",

"Description": "CloudFormation template for EC2 instance with web server",

"Parameters": {
    "InstanceType": {
        "Description": "WebServer EC2 instance type",
        "Type": "String",
        "Default": "t2.micro",
        "AllowedValues": ["t2.micro"],
        "ConstraintDescription": "Must be a valid EC2 instance."
    },

    "VpcId": {
        "Description": "VPC id",
        "Type": "String"
    },

    "InstanceSubnetId": {
        "Description": "Subnet id where instance would be hosted",
        "Type": "String"
    },

    "KeyName": {
        "Description": "Name of existing EC2 key-pair to enable SSH access to the instance",
        "Type": "String",
        "ConstraintDescription": "Must be the name of an existing EC2 keypair"
    },

    "SSHLocation": {
        "Description": "The IP address range that can be used to SSH to EC2 instances",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "0.0.0.0/0",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "Must be a valid IP CIDR range of the form x.x.x.x/x."

    }
},

"Mappings": {
    "AWSInstanceType2Arch":{
        "t2.micro": {
            "Arch": "HVM64"
        }
    },
    "AWSRegionArch2AMI": {
        "eu-west-1": {
            "HVM64": "ami-08eeb5a90cf59a66a"
        },
        "eu-west-2": {
            "HVM64": "ami-08eeb5a90cf59a66a"
        },
        "eu-west-3": {
            "HVM64": "ami-08eeb5a90cf59a66a"
        }
    }
},

"Resources": {
    "WebServerSecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties":{
            "VpcId": {
                "Ref": "VpcId"
            },

            "GroupDescription" : "Allow access from HTTP and SSH traffic",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "CidrIp": "0.0.0.0/0"    
                },

                {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "CidrIp": {
                        "Ref": "SSHLocation"
                    }
                }
            ]
            
        }
    },
    
    "WebServerEC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Metadata": {
            "AWS::CloudFormation::Init": {
                "configSets": {
                    "All": [
                        "ConfigureSampleApp"
                    ]
                },
                "ConfigureSampleApp": {
                    "packages": {
                        "yum": {
                            "httpd": []
                        }
                    },
                    "files": {
                        "/var/www/html/index.html": {
                            "content": { 
                                "Fn::Join": [
                                    "\n",
                                    [
                                        "<h1>Congratulations, you have successfully launched the AWS CloudFormation sample.</h1>"
                                    ]
                                ]
                            },
                            "mode": "000644",
                            "owner": "root",
                            "group": "root"
                        }
                    },

                    "services": {
                        "sysvinit": {
                            "httpd": { 
                                "enabled": "true", 
                                "ensureRunning" : "true" 
                            }
                        }
                    }
                }  
            }
        },
        
        "Properties": {
            "InstanceType": {
                "Ref": "InstanceType"
            },

            "KeyName": {
                "Ref": "KeyName"
            },

            "ImageId": {
                "Fn::FindInMap": [
                    "AWSRegionArch2AMI",
                    {
                        "Ref": "AWS::Region"
                    },
                    {
                        "Fn::FindInMap": [
                            "AWSInstanceType2Arch",
                            {
                                "Ref": "InstanceType"
                            },
                            "Arch"
                        ]
                    }
                ]
            },

            "NetworkInterfaces": [
                {
                    "Description": "Primary network interface",
                    "DeviceIndex": "0",
                    "SubnetId": {
                        "Ref": "InstanceSubnetId"
                    },
                    "GroupSet": [
                        {
                            "Ref": "WebServerSecurityGroup"
                        }
                    ]
                }
            ],

            "UserData": {
                "Fn::Base64": {
                    "Fn::Join": [
                        "",
                        [
                            "#!/bin/bash -xe\n",
                            "yum install -y aws-cfn-bootstrap\n",
                            "# Install the files and packages from the metadata\n",
                            "/opt/aws/bin/cfn-init -v ",
                            "         --stack ",
                            {
                                "Ref": "AWS::StackName"
                            },

                            "         --resource WebServerInstance ",
                            "         --configsets All ",
                            "         --region ",

                            {
                                "Ref": "AWS::Region"
                            },
                            "\n",
                            "# Signal the status from cfn-init\n",
                            "/opt/aws/bin/cfn-signal -e $? ",
                            "         --stack ",
                            {
                                "Ref": "AWS::StackName"
                            },
                            "         --resource WebServerInstance ",
                            "         --region ",
                            {
                                "Ref": "AWS::Region"
                            },
                            "\n"
                        ]
                    ]
                }
            }
        }
    }
}

}

【问题讨论】:

  • 你的EC2机器是linux还是windows? Linux 机器没有 IIS。
  • @ChetanRanpariya 啊,是的,对不起,我没有具体说明。这是窗户。
  • 您可能想检查它是否真的是 Windows 实例。Windows 服务器不支持 SSH。他们支持通过 3389 端口远程登录。此外,/var/www/html/yum 命令等位置也不支持 Windows。或者我错过了一些重要的东西。
  • @ChetanRanpariya 是的,我使用的 ami 是基于 Windows 的。我使用示例作为参考构建了我的模板。我假设 SSH 参数是在 ami 基于 linux 的情况下添加的功能。鉴于 ami 方面也是参数化的。

标签: amazon-web-services iis amazon-ec2 amazon-cloudformation


【解决方案1】:

首先,SSHLocation 参数应该被丢弃,因为这在设置 linux 实例时是相关的。无论在哪里引用,0.0.0.0/0 都是合适的替代品。

可以使用UserData 配置使用 IIS 设置您的 Windows 实例,该配置使用 Powershell 而不是基于 linux 的 bash。

"UserData": {
                "Fn::Base64": {
                    "Fn::Join": [
                        "",
                        [
                            "<powershell>\n",
                            "Add-WindowsFeature Web-WebServer -includeAllSubFeature -logpath $env:temp\\Web-WebServer_feature.log \n",
                            "Add-WindowsFeature Web-Mgmt-Tools -includeAllSubFeature -logpath $env:temp\\Web-Mgmt-Tools_feature.log \n",
                            
                            "remove-website -name \"Default Web Site\" \n",
                            "new-website -name site -port 80 -physicalpath C:\\inetpub\\wwwroot -ApplicationPool \".NET v4.5\" -force \n",
                            "</powershell>\n",
                            "<script>\n",
                            "cfn-init.exe -v -c setup -s ",
                            {
                                "Ref": "AWS::StackId"
                            },
                            " -r WebServerLC",
                            " --region ",
                            {
                                "Ref": "AWS::Region"
                            },
                            "\n",
                            "cfn-signal.exe -e %ERRORLEVEL% \"",
                            "\"",
                            "</script>\n"
                        ]
                    ]
                }
            }

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2021-03-21
    • 2017-04-16
    • 2020-09-02
    • 2019-09-02
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多