1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptでCloudFrontにドメインを割り当てる(AWS SDK)

Posted at

JavaScriptでEIPとドメインを関連付けるの続きです。
上の記事と同じ手順で、ホステッドゾーンIDを取得してからレコードセットを追加します。

var AWS = require('aws-sdk');

var cdn = "YOUR_CLOUDFRONT_DOMAIN";
var domain = "YOUR_DOMAIN";
addRecordSet( domain, cdn );

function addRecordSet( domain, cdn ) {
  var params = {
    DNSName: cdn,
    MaxItems: '1'
  };
  var route53 = new AWS.Route53({
    apiVersion: '2014-05-15',
    accessKeyId: "ACCESS_KEY_ID",
    secretAccessKey: "SECRET_ACCESS_KEY",
    region: "REGION"
  });
  route53.listHostedZonesByName(params).promise().then(
	(data) =>  {
      console.log('Find Your Domain.');
	  return data;
  }).then( (data) => {
	console.log('Upsert Your Domain.');
	var HostedZoneId = data.HostedZones[0].Id;
	HostedZoneId = HostedZoneId.split('/')[2];

	var params = {
	  ChangeBatch: {
	    Changes: [
	      {
	        Action: 'UPSERT',
	        ResourceRecordSet: {
	          Name: data.DNSName + '.',
	          Type: 'A',
			  AliasTarget: {
				  DNSName: cdn,
				  EvaluateTargetHealth: true,
				  HostedZoneId: "Z2FDTNDATAQYW2"
				},
	        }
	      }
	    ],
	  },
	  HostedZoneId: HostedZoneId
	};
	return route53.changeResourceRecordSets(params).promise();
  }).then( (data) => {
	console.log(data);
  }).catch( (err) => {
	  console.log('Fail to add domain');
	  console.log(err);
	  //context.fail(err);
  });
}

AliasTargetのHostedZoneIdはリソースによって固定値になります。
CloudFrontは"Z2FDTNDATAQYW2"を入れておきましょう。

成功した場合の戻り値

{ ChangeInfo: 
   { Id: '/change/C2PCQEAEVXLDCZ',
     Status: 'PENDING',
     SubmittedAt: Thu Jun 09 2016 15:54:53 GMT+0900 (JST) } }
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?