我有一个CloudFormation模板,可以为RStudio服务器启动EC2实例。
AWSTemplateFormatVersion: 2010-09-09
Description: Sets up an RStudio and Shiny environment on AWS
Parameters:
InstanceType:
Type: String
Description: Instance type for RStudio. Default is t2.micro.
AllowedValues:
- t2.micro
- t2.small
- t2.medium
- t2.large
- t2.xlarge
- t2.2xlarge
- m4.large
- m4.xlarge
- m4.2xlarge
- m4.4xlarge
- m4.10xlarge
- m4.16xlarge
- c4.large
- c4.xlarge
- c4.2xlarge
- c4.4xlarge
- c4.8xlarge
- r4.large
- r4.xlarge
- r4.2xlarge
- r4.4xlarge
- r4.8xlarge
- r4.16xlarge
- g2.2xlarge
- g2.8xlarge
- p2.xlarge
- p2.8xlarge
- p2.16xlarge
- g3.4xlarge
- g3.8xlarge
- g3.16xlarge
ConstraintDescription: Valid instance type in the t2, m4, c4, r4, g2, p2, and g3 families
Default: t2.micro
ImageId:
Type: AWS::EC2::Image::Id
Description: Amazon Linux Image ID. Default is for 2017.03.01 (HVM). N.B. If you want more storage then you should update your image
Default: ami-4fffc834
VpcId:
Type: AWS::EC2::VPC::Id
Description: VPC this server will reside in
InitialUser:
Type: String
Description: User Name for RStudio
InitialPassword:
Type: String
Description: Password for RStudio. Please keep in your records as this will not be echoed in the CloudFormation Console
NoEcho: True
RStatsS3Bucket:
Type: String
Description: Name of the S3 bucket
KeyPair:
Type: "AWS::EC2::KeyPair::KeyName"
Description: Amazon EC2 Key Pair
SubnetId:
Type: "AWS::EC2::Subnet::Id"
Description: Subnet ID your instance will launch in. Should be Internet accessible for the purposes of this demo.
Resources:
RStatsS3ReadPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: RStatsS3ReadPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "s3:ListBucket"
Resource:
- !Sub "arn:aws:s3:::${RStatsS3Bucket}"
- Effect: Allow
Action:
- "s3:PutObject"
- "s3:GetObject"
- "s3:DeleteObject"
Resource:
- !Sub "arn:aws:s3:::${RStatsS3Bucket}/*"
Roles:
- !Ref RStatsS3ReadRole
RStatsS3ReadRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
RstatsEC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
SecurityGroupIngress:
- CidrIp: "0.0.0.0/0"
FromPort: 22
ToPort: 22
IpProtocol: tcp
- CidrIp: "0.0.0.0/0"
FromPort: 8787
ToPort: 8787
IpProtocol: tcp
- CidrIp: "0.0.0.0/0"
FromPort: 3838
ToPort: 3838
IpProtocol: tcp
GroupDescription: RStudio and Shiny Security Group
VpcId: !Ref VpcId
RStatsEIP:
Type: AWS::EC2::EIP
Properties:
Domain: !Ref VpcId
RStatsEIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt RStatsEIP.AllocationId
NetworkInterfaceId: !Ref RStatsNetworkInterface
RStatsNetworkInterface:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: !Ref SubnetId
Description: Interface for RStudio Connection
GroupSet:
- !Ref RstatsEC2SecurityGroup
SourceDestCheck: true
RStatsEC2Instance:
Type: AWS::EC2::Instance
DependsOn: RStatsEIPAssociation
Properties:
ImageId: !Ref ImageId
InstanceType: !Ref InstanceType
KeyName: !Ref KeyPair
NetworkInterfaces:
- NetworkInterfaceId: !Ref RStatsNetworkInterface
DeviceIndex: 0
Tags:
- Key: Name
Value: RStudio
UserData:
Fn::Base64: !Sub |
#!/bin/bash
#install R
yum install -y R
#install RStudio-Server 1.0.153 (2017-07-20)
wget https://download2.rstudio.org/server/centos6/x86_64/rstudio-server-rhel-1.2.1335-x86_64.rpm
yum install -y --nogpgcheck rstudio-server-rhel-1.2.1335-x86_64.rpm
rm rstudio-server-rhel-1.2.1335-x86_64.rpm
#install shiny and shiny-server (2017-08-25)
R -e "install.packages('shiny', repos='http://cran.rstudio.com/')"
wget https://download3.rstudio.org/centos6.3/x86_64/shiny-server-1.5.9.923-x86_64.rpm
yum install -y --nogpgcheck shiny-server-1.5.9.923-x86_64.rpm
#add user(s)
useradd ${InitialUser}
echo ${InitialUser}:${InitialPassword} | chpasswd
# install curl
yum install -y curl-devel
# Use Shiny server
/opt/shiny-server/bin/deploy-example user-dirs
mkdir /home/${InitialUser}/ShinyApps
# copy in some test data
cp -R /opt/shiny-server/samples/sample-apps/hello /home/${InitialUser}/ShinyApps/
Outputs:
RStudioURL:
Value: !Join [":", [!GetAtt RStatsEC2Instance.PublicDnsName, "8787"]]
ShinyStudioURL:
Value: !Join [":", [!GetAtt RStatsEC2Instance.PublicDnsName, !Sub "3838/${InitialUser}/MyApp"]]
PublicIp:
Value: !GetAtt RStatsEC2Instance.PublicIp
我使用此模板优化了五个实例。我想再旋转几个,但是遇到了这个错误。
The maximum number of addresses has been reached. (Service: AmazonEC2; Status Code: 400; Error Code: AddressLimitExceeded; Request ID: 6c8acda5-b399-4239-aa30-265a4371bc6d)
我尝试从Amazon池中创建更多IP地址,但遇到此错误。
The maximum number of addresses has been reached.
我可以重复使用我已经拥有的地址吗?
答案 0 :(得分:0)
弹性IP的主要用例是在实例故障的情况下将IP从一个实例移动到另一个实例,作为HA解决方案。
如果您只需要RStudio实例可公开访问,则在启动时启用公共IP(不是弹性IP)。使用相关AWS::EC2::NetworkInterface上的AssociatePublicIpAddress
来完成此操作。